2016-01-06 53 views
-6

的,而當VAR [5]從「轉變」,「左」或「右」的Python而當變量改變

#Variable Input 
name = input("Enter your name: ") 
place = input("Enter where you live: ") 
sex = input("Are you a boy (Y/N): ") 
scaryanimal = input("Enter the type of animal that you are most scared of: ") 
happyanimal = input("Enter your favourite type of animal: ") 
var=[name,place,sex,scaryanimal,happyanimal,""] 

#Main story 

#Other code goes here 

#Offending While loop 

while var[5].lower() != "left" or var[5].lower() != "right": 
     var[5] = input("Did he go left or right: ") 
     print(var[5]) 
+1

我沒有看到你改變'變種[5] '。如果你認爲'vari = input(...)'改變了'var'的內容,你可能會誤解列表分配的語法。 – Kevin

+0

糟糕,只是意識到我忘記用var [5]替換vari,我將var [5]更改爲變量以檢查它是否是錯誤的數組。 :-( –

+0

是你的原始代碼「正確」與'var [5]'而不是'vari'? – tglaria

回答

1

我看到你說你的變量變化的循環不會停止不結束循環, 但是哪裏?

有兩件事:你必須改變你正在檢查的變量,並且你必須改變那個條件測試,while循環條件是True當變量var[5]與'left'不同或'right'不同時(如果它的「左」,那麼它比「正確」的不同,所以循環將繼續)

所以......

while var[5].lower() != "left" and var[5].lower() != "right": 
    var[5] = input("Did he go left or right: ") 
    print(var[5])