微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OR语句处理两个!=子句Python

如何解决OR语句处理两个!=子句Python

| (使用Python 2.7)我理解这是很基本的,但是为什么以下语句不能按书面形式工作:
input = int(raw_input())
while input != 10 or input != 20:
    print \'Incorrect value,try again\'
    bet = int(raw_input())
基本上我只想接受10或20作为答案。现在,无论\'input \',甚至10或20,我都得到\'Incorrect value \'。这些条款会自相矛盾吗?我认为只要其中一个子句是正确的,OR语句就会说“确定”。谢谢!     

解决方法

您需要
and
while input != 10 and input != 20:
仔细想想:如果
input
10
,则第一个表达式是
false
,从而使Python评估第二个表达式
input != 20
10
是form8ѭ的不同形式,因此此表达式的计算结果为
true
。作为
false or true == true
,整个表达式为
true
20
也一样。     ,....或其他表达方式,对您来说似乎更自然:
while input not in (10,20):
    # your code here...
    ,您的意思是让
bet
input
。我认为您的意思是说输入是否为10而不是20。
input = int(raw_input())
while input != 10 and input != 20:
    print \'Incorrect value,try again\'
    input = int(raw_input())
    ,我想你要那儿一英镑。
while input != 10 or input != 20:
这将永远重复-如果
input
为10,则第一个条件为假。如果
input
为20,则第二个条件为假。
input
不能同时是10和20,因此等于
true
。     ,您需要\“ and \”,而不是\“ or \”。考虑一下您的布尔逻辑。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。