python中While循环的多个或多个条件
忘记了如何在while循环中声明多个条件 它将获取其中一个输入,并检查用户是否输入了4个正确字母中的任意一个。然而,目前它总是进入循环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?"
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个月没有编码了,事情变得太复杂了。这就解决了问题,只要我能接受这个答案