0

我对python编程比较陌生,无论如何这是一大段代码的一小部分。这似乎导致了问题:

command = input("Command: ")

while command != ("Exit lift" or "Up" or "Down" or "1" or "2" or "3" or "Cycle"):
    print("Error: Command entered doesn't match the 'Commands' list, or isn't a possible command at this time! Please try again...")
    command = input("Command: ")

print ("Works")

基本上,我测试了这些命令,它只接受“Exit lift”命令,以及“Up”、“Down”、“1”...等。不会工作。

有什么建议么?初学者

4

3 回答 3

3

("Exit lift" or "Up" or "Down" or "1" or "2" or "3" or "Cycle")被评估为'Exit lift'

>>> ("Exit lift" or "Up" or "Down" or "1" or "2" or "3" or "Cycle")
'Exit lift'

所以command != ("Exit lift" or "Up" or "Down" or "1" or "2" or "3" or "Cycle"):等价于command != ("Exit lift")

not in与序列一起使用:

while command not in ("Exit lift", "Up", "Down", "1", "2", "3", "Cycle"):
    ....
于 2013-10-14T09:39:21.513 回答
0

代替

while command != ("Exit lift" or "Up" or "Down" or "1" or "2" or "3" or "Cycle"):

你应该使用

while not command in ("Exit lift", "Up", "Down", "1", "2", "3", "Cycle"):
于 2013-10-14T09:40:28.087 回答
0

你可以使用一个数组,并使用这个in

allowed = ["Exit lift", "Up", "Down", "1", "2", "3", "Cycle"]

while command not in allowed:
    print "not allowed!"

print "works"
于 2013-10-14T09:44:08.753 回答