我想在循环时制作一个简单的汽车游戏

发布于 2025-02-10 15:01:36 字数 608 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭了丶谁疼 2025-02-17 15:01:36

您可以使用简单的标志is_car_started记录汽车是否已经启动的状态。启动汽车时,将设置为_CAR_STARTED true。当您停止汽车时,将其设置为假。

command=""
is_car_started = False
while True:
    command=input('>').lower()
    if command=='start':
        if is_car_started == True:
            print("Car started already")
        else:
            is_car_started = True
            print("Car started")
    elif command=='stop':
         is_car_started = False
         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")

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, set is_car_started to True. When you stop the car, set it to false.

command=""
is_car_started = False
while True:
    command=input('>').lower()
    if command=='start':
        if is_car_started == True:
            print("Car started already")
        else:
            is_car_started = True
            print("Car started")
    elif command=='stop':
         is_car_started = False
         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")
善良天后 2025-02-17 15:01:36

您可以在循环外部定义布尔值变量。像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 set first_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:...

又怨 2025-02-17 15:01:36

您需要跟踪是否启动汽车。您还可以使用匹配/案例实现与代码相比的较高结构(需要Python 3.10)。例如:

started = False
while True:
    match input('Enter command: ').lower():
        case 'start':
            if started:
                print('Already started')
            else:
                print('Car started')
                started = True
        case 'stop':
            if started:
                print('Car stopped')
                started = False
            else:
                print('Car not started')
        case 'quit':
            print('Goodbye')
            break
        case 'help':
            print('''
                start-to start the car
                stop-to stop the car
                quit-to exit
                ''')
        case _:
            print("I don't understand that")

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:

started = False
while True:
    match input('Enter command: ').lower():
        case 'start':
            if started:
                print('Already started')
            else:
                print('Car started')
                started = True
        case 'stop':
            if started:
                print('Car stopped')
                started = False
            else:
                print('Car not started')
        case 'quit':
            print('Goodbye')
            break
        case 'help':
            print('''
                start-to start the car
                stop-to stop the car
                quit-to exit
                ''')
        case _:
            print("I don't understand that")
雨的味道风的声音 2025-02-17 15:01:36

我也有类似的问题,这就是我解决的问题
希望它有帮助...

    if is_car_started == True:
     print("car started already...")
    else:
     is_car_started = True
     print("car started... ready to go")

i had a similar issue and this is how i resolved it
hope it helps...

    if is_car_started == True:
     print("car started already...")
    else:
     is_car_started = True
     print("car started... ready to go")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文