繁体   English   中英

如何检查命令是否已经执行

[英]How to check if a command has already been executed

问题是:我不知道如何编写脚本,因此如果我多次键入 start 或 stop 脚本将打印诸如“已在运行”或“未运行”之类的内容

running = True
print("Type help for a list of commands. ")
while running :
    user=input("> ")
    user_input=user.upper()
    if user_input==("HELP"):
        print(f"""Type start to start the car.
Type stop to stop the car.
Type quit to quit the game.""")
    elif user_input==("START"):
        print("You started the car. ")
    elif user_input==("STOP"):
        print("You stopped the car. ")
    elif user_input==("QUIT"):
        print("You stopped the game.")
        running=False
    elif user_input!=("START") and user_input!=("STOP") and user_input!=("QUIT"):
        print("I don't understand that. ")

例子:

 >start
You started the car.
 >start
Car is already running.
 >stop
You stopped the car.
 >stop
Car isn't turned on.

首先,如果你想停车,你必须先启动它,对吗?

创建一个全局变量——我们称之为started ——并将其设置为False 现在,当我们想要“启动”汽车时,首先我们需要检查:

elif user_input==("START"):
    if started:
        print("Car is already running")
    else:
        print("You started the car")
        started = True

然后就停止类似的声明:如果startedTrue ,那么这意味着你的车正在运行,你可以停下来。 started设置为False并打印汽车停止的消息。 否则,你甚至没有打开汽车。

PS 注意:在 while 循环之前声明和初始化started

暂无
暂无

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

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