在 python 中使用 while 循环的交通灯程序

问题描述 投票:0回答:3

我想在python中运行这段代码,我希望代码在用户输入30时停止循环,这里我使用break来停止循环,还有其他方法吗?任何建议都会有所帮助。

`x = input("Enter value: ")
stop_light = int(x)
while True:
    if stop_light >= 1 and stop_light < 10:
        print('Green light')
        stop_light += 1
    elif stop_light < 20:
        print('Yellow light')
        stop_light += 1
    elif stop_light < 30:
        print("Red light")
        stop_light += 1
    else:
        stop_light = 0
    break`
traffic light
3个回答
0
投票

嗯,我不太确定你到底想达到什么目的。您的版本不会循环,因为在第一次迭代后总是会遇到 break 语句。 此外,在循环实际开始之前,您只需要一次用户输入。

这就是我想你想要做的,所以我将用户输入部分移到循环内,并将中断条件添加到 pythonic“switch”语句中。只要用户不输入 30,这只会要求用户输入。

while True:
    x = input("Enter value: ")
    stop_light = int(x)
    if stop_light == 30:
        break
    elif stop_light >= 1 and stop_light < 10:
        print('Green light')
        stop_light += 1
    elif stop_light < 20:
        print('Yellow light')
        stop_light += 1
    elif stop_light < 30:
        print("Red light")
        stop_light += 1
    else:
        stop_light = 0

0
投票

正如我已经提到的,您的代码不会循环。然而,这段代码确实:

while True:
    try:
        x = input("Enter value: ")
        stop_light = int(x)
    except ValueError:
        print("Try Again")
    else:
        break

while stop_light <= 30:
    if stop_light >= 1 and stop_light < 10:
        print('Green light')
    elif stop_light < 20:
        print('Yellow light')
    elif stop_light < 30:
        print("Red light")
    stop_light += 1

在这里,我在 while 循环中使用了一个条件。条件是循环迭代用户值,直到它变成

30
。之所以会这样,是因为你在
Red light
线的情况。我在下面的问题#2中进一步解释了这一点。

在继续之前请注意我正在捕捉

ValueError

也只有一个

stop_light += 1
.

以下是一些示例输出:

Enter value: asdf
Try Again
Enter value: 27
Red light
Red light
Red light
# Breaks and closes the code.

Enter value: 5
Green light
Green light
Green light
Green light
Green light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Yellow light
Red light
Red light
Red light
Red light
Red light
Red light
Red light
Red light
Red light
Red light
# Breaks and closes the code.

现在,这些是您的代码存在的问题:

问题#1

else:
    stop_light = 0

这部分没有正确处理负数和大于

30
的数字。我认为您不想为属于该条件的值迭代 30 次,因为您没有在代码中应用
stop_light += 1
。所以,你肯定要想想这样做的目的是什么

查看下面不断循环直到值大于

30
的代码:

while True:
    try:
        x = input("Enter value: ")
        stop_light = int(x)
    except ValueError:
        print("Try Again")
    else:
        while stop_light < 30:
            if stop_light >= 1 and stop_light < 10:
                print('Green light')
            elif stop_light < 20:
                print('Yellow light')
            elif stop_light < 30:
                print("Red light")
            stop_light += 1
        if stop_light > 30:
            break

Output:

Enter value: asdf
Try Again
Enter value: 27
Red light
Red light
Red light
Enter value: 31
# Breaks and closes the code.

问题#2

我编写脚本循环直到值大于 30 的原因是因为您的代码中有以下几行:

elif stop_light < 30:
    print("Red light")
    stop_light += 1

如果您希望循环仅在用户输入

30
时中断,那么上面的那些代码行与该理论背道而驰。由于
stop_light
的增加,当
29
stop_light
时,您的循环将收支平衡。您还应该研究一下您的逻辑缺陷。


0
投票

虽然正确: x = input("输入值(q 退出):") 如果 x == 'q': 休息 停止灯 = int(x)

while True:
    if stop_light >= 1 and stop_light < 10:
        print('Green light')
        stop_light += 1
    elif stop_light < 20:
        print('Yellow light')
        stop_light += 1
    elif stop_light < 30:
        print("Red light")
        stop_light += 1
    else:
        stop_light = 0
    if stop_light == 0:
        break
© www.soinside.com 2019 - 2024. All rights reserved.