如果我 运行 我的代码运行良好,但我希望它在终端中第二次输入 start 时显示不同的输出

If I run my code it works well but I want it to show me a different output when I input start for the second time in the terminal

在运行时

代码的行为异常。当我说开始时,它显示我的汽车已准备就绪 但是当我说重新开始它直到显示相同的输出 我希望代码在我说第二次启动时显示汽车已经启动

command = ""
while command != "quit":
    unit = input("> ").lower()
    if unit == "start":
        print ("car started ... ready to go")
    elif unit == "stop":
        print("car stopped")
    elif unit == "help":
        print("""
start - start the car
stop - stop the car
quit - exit
        """)
    elif unit == "quit":
        break
    else:
        print("sorry I don't understand") 

为了实现这一点,您将需要一些状态来存储汽车当前所处的状态。在您的情况下,您可以简单地使用相应设置的标志 car_running

car_running = False

command = ""
while command != "quit":
    unit = input("> ").lower()
    if unit == "start":
        if car_running:
            print("car has already been started")
        else:
            print("car started ... ready to go")
            car_running = True
    elif unit == "stop":
        if car_running:
            print("car stopped")
            car_running = False
        else:
            print("car is not running")
    elif unit == "help":
        print(""" start - start the car stop - stop the car quit - exit """)
    elif unit == "quit":
        break
    else:
        print("sorry I don't understand")

如果你有多个州,汽车可以在你可以使用 enum

你的建筑本质上是一个finite-state machine