我想在循环时制作一个简单的汽车游戏
1-当我们输入帮助时,应显示:
启动开始汽车 停止停车 退出出口
2-当我们输入开始消息时:应显示汽车的启动
3-进入停止时:应显示停止的汽车
4-当输入退出时...应该通过循环
5-我们不能 退出...启动汽车两次或更多 - 像汽车一样启动的消息也应该在停车 我的代码:
command=""
while True:
command=input('>').lower()
if command=='start':
print("Car started")
elif command=='stop':
print("Car stopped")
elif command=="help":
print('''
start-to start the car
stop-to stop the car
quit-to exit
''')
elif command=='quit':
break
else:
print("I don't understand that")
我做了这些部分,但无法阻止汽车启动两次。帮助 :)
1--when we enter help following should appear:
start-to start the car
stop-to stop the car
quit-to exit
2--when we enter started message : car started should be shown
3--when entered stop: Car stopped should be displayed
4--when entered quit...should be exited through the loop
5--we cannot start car two times or more --message like car started already should be shown same with the stop
my code:
command=""
while True:
command=input('>').lower()
if command=='start':
print("Car started")
elif command=='stop':
print("Car stopped")
elif command=="help":
print('''
start-to start the car
stop-to stop the car
quit-to exit
''')
elif command=='quit':
break
else:
print("I don't understand that")
I did these part but was unable to prevent the car from starting twice. Help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用简单的标志
is_car_started
记录汽车是否已经启动的状态。启动汽车时,将设置为_CAR_STARTED
true。当您停止汽车时,将其设置为假。You can use a simple flag
is_car_started
to record the status of whether the car is already started or not. When you start the car, setis_car_started
to True. When you stop the car, set it to false.您可以在循环外部定义布尔值变量。像
first_start = true
。然后在if语句中内部循环内部检查
命令==“ start”
您可以将first_start
设置为false。在顶部,您可以添加一个if-statement,当
first_start == false
时打印消息。if-statement看起来像这样:
如果不是first_start:...
you can define a boolean variable outside your while loop. Like
first_start = true
.Then inside your while loop in the if statement where you check if
command=="start"
you can setfirst_start
to false.On the very top you can add an if-statement that prints your message when
first_start == false
.The if-statement would look something like this:
if not first_start:...
您需要跟踪是否启动汽车。您还可以使用匹配/案例实现与代码相比的较高结构(需要Python 3.10)。例如:
You need to keep track of whether or not the car is started. You can also achieve a superior structure to your code with match/case (requires Python 3.10). For example:
我也有类似的问题,这就是我解决的问题
希望它有帮助...
i had a similar issue and this is how i resolved it
hope it helps...