如何修复在 Python 中永远持续的 while 循环? [英] How to fix a while loop that goes on forever in Python?

查看:28
本文介绍了如何修复在 Python 中永远持续的 while 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的 while 循环来运行命令 start、stop、quit 和 help.开始、停止和帮助将只显示一些打印文本.在它们运行后,我希望它继续执行另一个命令.但是,退出时我希望整个程序停止.

I am trying to create a simple while loop that will run the commands start, stop, quit, and help. 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()

这篇关于如何修复在 Python 中永远持续的 while 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆