简体   繁体   English

如何修复在 Python 中永远持续的 while 循环?

[英]How to fix a while loop that goes on forever in Python?

I am trying to create a simple while loop that will run the commands start, stop, quit, and help.我正在尝试创建一个简单的 while 循环,它将运行命令启动、停止、退出和帮助。 Start, stop, and help will just display some printed text.开始、停止和帮助只会显示一些打印文本。 After they are run, I want it to keep going on to another command.在它们运行之后,我希望它继续执行另一个命令。 However, on quit I want the whole program to stop.但是,在退出时,我希望整个程序停止。

input_command = input("> ").lower()

while input_command != "quit":
    print(input_command)
    if input_command == "start":
        print("The car is ready! VROOM VROOM!")
        print(input_command)
    elif input_command == "stop":
        print("Car stopped.")
    elif input_command == "help":
        print("""
        start - starts the car
        stop - stops the car
        quit - exits the program
        """)
else:
    print("Sorry, I don't understand that...")

You never reassign input command so it only ever takes input once,您永远不会重新分配输入命令,因此它只需要输入一次,

input_command = ''

while input_command != "quit":
     input_command = input("> ").lower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM