问题
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