2013-10-14 71 views
0

我對python編程比較新,反正這是一小段代碼。這似乎是導致問題: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") 

基本上,我測試的命令,並只選擇向上的「退出升降機」命令,「向上」,「向下」,「1」 ...等等將無法工作。

有什麼建議嗎?初學者

回答

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"): 
+0

我正要張貼了同樣的答案:) –

+0

謝謝,你的答案是有益的和簡單的。乾杯。 – user2878311

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"): 
    .... 
0

你可以使用一個數組,並使用in

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

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

print "works"