我们可以在python中的语句中使用多少逻辑运算符?

时间:2014-11-28 04:03:37

标签: python

vari1='a'
# choices are  all ready printed above.
while vari1 != 'o' and vari1 != 'O' and vari1 != 'p' and vari1 != 'P':
      vari1=input("please enter your choice.")
      if vari1 != 'o' and vari1 != 'O' and vari1 != 'p' and vari1 != 'P':
            print("please enter a appropriate choice") 

我尝试了这段代码,但它没有用。我想知道我可以在一个语句中使用多少逻辑运算符。我认为我的病情有些问题。

1 个答案:

答案 0 :(得分:1)

最好以这种方式使用:

while True:
    vari1=input("please enter your choice.")
    if vari1.lower() not in ['o','p']:
        print("please enter a appropriate choice") 
    else: 
        # do your stuff 
        break

str.lower将字符串转换为小写。

演示:

>>> 1 in [1,2,3,4,5]
True
>>> 1 not in [1,2,3,4,5]
False
>>> 1 in [2,3,4,5]
False
>>> 1 not in [2,3,4,5]
True