gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:48 25 4
gpt4 key购买 nike

这就是问题所在:我不知道如何编写脚本以便如果我多次键入启动或停止脚本将打印类似“已经运行”或“未运行”的内容

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 并打印汽车停止的消息。否则,你连车都没开。

附言注意:在 while 循环之前声明并初始化 started!

关于python - 如何检查命令是否已经执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com