Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中While循环的多个或多个条件_Python - Fatal编程技术网

python中While循环的多个或多个条件

python中While循环的多个或多个条件,python,Python,忘记了如何在while循环中声明多个条件 它将获取其中一个输入,并检查用户是否输入了4个正确字母中的任意一个。然而,目前它总是进入循环 temp = input() while temp.lower() != ("a" or "b" or "c" or "d"): print("Error, you must enter either A, B, C or D") print(inventory()) print("What would you like to buy?"

忘记了如何在while循环中声明多个条件

它将获取其中一个输入,并检查用户是否输入了4个正确字母中的任意一个。然而,目前它总是进入循环

temp = input()
while temp.lower() != ("a" or "b" or "c" or "d"):
    print("Error, you must enter either A, B, C or D")
    print(inventory())
    print("What would you like to buy?")
    temp = input()

明确地说,你必须写作

while (temp.lower() != "a") and (temp.lower() != "b")....... :
但你可能想试试

while temp.lower()[0] not in 'abcd':

但是,请注意我在这里添加到降低的输入变量的索引,否则长度大于1的
'abcd'
的子字符串也会匹配。

明确地说,您必须编写

while (temp.lower() != "a") and (temp.lower() != "b")....... :
但你可能想试试

while temp.lower()[0] not in 'abcd':

但是,请注意我在这里添加到降低的输入变量的索引,否则长度大于1的
'abcd'
的子字符串也会匹配。

您可能需要考虑

while temp.lower() not in ('a','b','c','d'):
    print("Error")
不知道为什么会这样做,但仍然有一个选择:

import string
while temp.lower() not in string.ascii_lowercase[:4]:
    print("Error")

你可能想考虑一下

while temp.lower() not in ('a','b','c','d'):
    print("Error")
不知道为什么会这样做,但仍然有一个选择:

import string
while temp.lower() not in string.ascii_lowercase[:4]:
    print("Error")

答:是的,我已经6个月没有编码了,而且事情太复杂了。这就解决了问题,只要我能接受这个答案,嗯,是的,我已经6个月没有编码了,事情变得太复杂了。这就解决了问题,只要我能接受这个答案