Home > Blockchain >  I want to make a simple car game using while loop
I want to make a simple car game using while loop

Time:06-27

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

CodePudding user response:

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

CodePudding user response:

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:...

CodePudding user response:

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

CodePudding user response:

command = ""
started = False
while True:
    command = input('>').lower()
    if command == 'start' and not started:
        print("Car started")
        started = True
        
    elif command == "start" and started: # Started already!
        print("Can't start twice!")
        
    elif command == 'stop':
        print("Car stopped")
        started = False # Allow to start again
        
    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")

Pretty simple. Add a flag

  • Related