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

时间:2018-01-10 23:20:16

标签: traffic light

我正在尝试在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`

2 个答案:

答案 0 :(得分:0)

是的,我不太确定你真正希望实现的目标。您的版本不会循环,因为在第一次迭代后将始终满足break语句。 此外,您只需在循环实际开始之前请求用户输入一次。

这是我想要做的,所以我将用户输入部分移动到循环中,并将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

答案 1 :(得分: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的数字。由于您未在代码中应用stop_light += 1,因此我不认为您希望对属于该条件的值进行30次迭代。所以,你一定要考虑这个目的是什么。

查看以下代码,这些代码会一直循环,直到值大于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的增加,29stop_light时,您的循环会收支平衡。你也应该研究一下你的逻辑缺陷。

相关问题