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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 01:46:34

问题


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...")

回答1:


You never reassign input command so it only ever takes input once,

input_command = ''

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


来源:https://stackoverflow.com/questions/58343621/how-to-fix-a-while-loop-that-goes-on-forever-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!