简体   繁体   English

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

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

Here's the problem : I don't know how to write the script so that if I type more than once start or stop the script will print something like "Already running" or "Not running"问题是:我不知道如何编写脚本,因此如果我多次键入 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. ")

Example:例子:

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

First of all, if you want to stop the car, you must start it first, right?首先,如果你想停车,你必须先启动它,对吗?

Make one global variable—let's call it started —and set it to False .创建一个全局变量——我们称之为started ——并将其设置为False Now, when we want to "start" the car, first we need to check:现在,当我们想要“启动”汽车时,首先我们需要检查:

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

Then just do the similar statement with stopping: if started is True , then it means your car is running and you can stop it.然后就停止类似的声明:如果startedTrue ,那么这意味着你的车正在运行,你可以停下来。 Set started to False and print the message that car is stopped.started设置为False并打印汽车停止的消息。 Otherwise, you didn't even turn on the car.否则,你甚至没有打开汽车。

PS Be careful: declare and initialize started before your while loop! PS 注意:在 while 循环之前声明和初始化started

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

相关问题 如何检查以前是否已使用过数据 - How to check if data has already been previously used 如何检查输入是否已经输入到txt文件中 - How to check if the input has already been entered into a txt file 一旦我的代码已经执行,如何让我的代码循环回输入? (角色扮演游戏) - How do I get my code to loop back to an input once it has been executed already? (RPG Game) 如何检查tensorflow图中是否已执行`compute_gradients`操作? - How to check if `compute_gradients` operation has been executed in tensorflow graph? 在执行命令之前,Python 3 TKinter窗口将不会打开 - Python 3 TKinter Window will not open until command has been executed Scikit-学习如何检查模型(例如TfidfVectorizer)是否已经适合 - Scikit-learn how to check if model (e.g. TfidfVectorizer) has been already fit Pygame - 如何检查我的rect是否已被点击? - Pygame - How do I check if my rect has already been clicked? 如何在没有引导加载程序的情况下检查固件是否已上传到 Arduino Leonardo? - How to check if firmware has already been uploaded to a Arduino Leonardo, without the bootloader? 如何输出输入已经输入 - How to output that the input has already been entered 检查是否已按下“显示更多”链接 python selenium - check if "show more" link has already been pressed python selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM