Python-ELIF语句不会继续WHILE循环

时间:2019-02-01 18:23:30

标签: python

在课堂上,我们从基础程序开始,并将继续在此基础上进行构建。我被困住了,不确定为什么程序到达ELIF语句时不会像第一个IF语句那样返回WHILE循环的开头

print("Great Scott! Marty we need to go back to the future!")
print("Marty, lets do a checklist!")
engine = None
circuit = None

while (engine !=1 and circuit != 1):
    engine = int(input("Engine on or off? Enter 1 for ON or 0 for OFF\n"))
    circuit = int(input("Circuit on or off? Enter 1 for ON or 0 for OFF\n"))

    if (engine == 0 and circuit == 0):
         print("Marty! we have to turn everything on before we can time travel!")
         print("Lets check again!")

    elif (engine == 1 and circuit == 0):
        print("Lets turn on the time cicruit")
        print("Lets check again!")

    elif (engine == 0 and circuit == 1):
        print("Turn on the engine!")
        print("Lets check again!")

    else:
        print("Great! lets start driving")
        speed = 0
        while speed < 88:
            speed = int(input("Whats our current speed?\n"))

            if speed < 88:
                print("We need to go faster!")

            else:
                print("Flux Capacitor Fully Charged")
                print("Marty, where we're going, we dont need roads!")

4 个答案:

答案 0 :(得分:1)

实际上,只需将更改为

while (engine !=1 or circuit != 1):

答案 1 :(得分:0)

您应该将变量enginecircuit重新启动为0。循环要求变量必须不同于1才能继续运行。

立即尝试:

print("Great Scott! Marty we need to go back to the future!")
print("Marty, lets do a checklist!")
engine = None
circuit = None

    while (engine !=1 and circuit != 1):
        engine = int(input("Engine on or off? Enter 1 for ON or 0 for OFF\n"))
        circuit = int(input("Circuit on or off? Enter 1 for ON or 0 for OFF\n"))

        if (engine == 0 and circuit == 0):
             print("Marty! we have to turn everything on before we can time travel!")
             print("Lets check again!")

        elif (engine == 1 and circuit == 0):
            print("Lets turn on the time cicruit")
            print("Lets check again!")
            engine = 0
            circuit = 0

        elif (engine == 0 and circuit == 1):
            print("Turn on the engine!")
            print("Lets check again!")
            engine = 0
            circuit = 0
        else:
            print("Great! lets start driving")
            speed = 0
            while speed < 88:
                speed = int(input("Whats our current speed?\n"))

                if speed < 88:
                    print("We need to go faster!")

                else:
                    print("Flux Capacitor Fully Charged")
                    print("Marty, where we're going, we dont need roads!")

答案 2 :(得分:0)

while (engine !=1 and circuit != 1):

上面的语句表示引擎和电路都不应该等于1。如果其中一个等于1,则条件求值为False并结束循环。

现在,在您的elif情况下,引擎或电路为1,因此循环条件的值为False,循环停止。

答案 3 :(得分:0)

from itertools import product
list(product(sti1,sti2))
[('death', 'glass'), ('death', 'book'), ('pain', 'glass'), ('pain', 'book')]

应更改为:

while (engine !=1 and circuit != 1):

在第一种情况下(带有while (engine !=1 or circuit != 1): 的引擎)仅在引擎和电路都不等于1时才继续循环。(某些伪代码:and而带有{{1} }这将是do something while condition 1 is true AND while condition 2 is true)。