Python变量时循环不会结束

时间:2016-01-06 13:17:13

标签: python loops python-3.x while-loop

当var [5]从""更改时,while循环不会停止到"离开"或"对"

#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 个答案:

答案 0 :(得分:1)

我读到你说你的变量正在改变,但在哪里?

两件事:您必须更改您正在检查的变量并且您必须更改该条件测试,当变量Truevar[5]时,循环条件为while var[5].lower() != "left" and var[5].lower() != "right": var[5] = input("Did he go left or right: ") print(var[5]) 不同于' left'或者不正确的' (如果它"离开",那么它与'右边'不同,所以循环将继续)

因此...

function pushAtStarting(array, element){
    array.unshift(element); // new element has s.no = 0
    for (index in array){
       array[index].sno++
    }
}
var array = [];
pushAtStarting(array, {sno: 0, description: "abc"});