[英]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.然后就停止类似的声明:如果
started
是True
,那么这意味着你的车正在运行,你可以停下来。 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.