【问题标题】:I want to make a simple car game using while loop我想使用 while 循环制作一个简单的汽车游戏
【发布时间】:2023-02-17 20:26:22
【问题描述】:

1--当我们输入help时应该会出现:

start-启动汽车 停止-停止汽车 quit-退出

2--当我们输入启动消息时:应该显示汽车启动

3--进入stop时:应显示car stopped

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")

我做了这些部分,但无法阻止汽车启动两次。帮助 :)

【问题讨论】:

    标签: python loops while-loop


    【解决方案1】:

    您可以使用一个简单的标记is_car_started 来记录汽车是否已经启动的状态。启动汽车时,将is_car_started 设置为 True。停车时,将其设置为 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")
    

    【讨论】:

      【解决方案2】:

      您可以在 while 循环之外定义一个布尔变量。喜欢first_start = true

      然后在 if 语句的 while 循环中检查 command=="start" 是否可以将 first_start 设置为 false。

      在最顶部,您可以添加一个 if 语句,在 first_start == false 时打印您的消息。

      if 语句看起来像这样:if not first_start:...

      【讨论】:

        【解决方案3】:

        您需要跟踪汽车是否启动。您还可以使用 match/case(需要 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")
        

        【讨论】:

          【解决方案4】:

          我有一个类似的问题,这就是我解决它的方法 希望能帮助到你...

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

          【讨论】:

            猜你喜欢
            • 2011-10-29
            • 1970-01-01
            • 1970-01-01
            • 2015-01-16
            • 1970-01-01
            • 2016-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多