简体   繁体   中英

Python Boolean Value in Car Game

I'm trying to code a simple car game, please see the below code, especially these lines: started = False, if started, started = True, if not started, started = False

My question is: I understand the definition of Boolean but I just can't wrap my head around it in this code. In the beginning, the car is stopped, the engine is not running so 'started = False', I get that. Then we have 'if started' and this is where the problem begins: 'if started' what? Because started is set to False, so I understand that like 'if started applies, then...' but that does not make sense in this code because that would mean, that if the value of started is correct, that would mean False, which would mean that the car has not started but in the code, under this statement, the car is already started.

And then again, 'if not started' to me means negative of 'started = False', which is True but in this case the car did not start.

Can you please explain someone? Thank you

command = ''
started = False
while True:
    command = input('> ').lower() #Python is case sensitive, we can declare '.lower()' just once
    if command == 'start':
        if started:
            print('Car already started!')
        else:
            started = True
            print('Car started')
    elif command == 'stop':
        if not started:
            print('Car already stopped!')
        else:
            started = False
            print('Car stopped')
    elif command == 'help':
        print("""
start - start the car
stop - stop the car
help - show guide
exit - exit the game
""")
    elif command == 'exit':
        break

In this scenario the car is stopped. So started == False When you type the command start the program checks first if the car is already started and if so it prints "Car already started!", as the car in this scenario is not started it skips that block and sets started to True .

Now if you type start again, started is already set to True , so this time it will print "Car already started!" and skip the rest of the code in the else block.

The same process is repeated for the stop command, it first checks the state of the car and either prints a message or changes the state

I'll paste your code down below and add comments to each line of code explaining what it does:

command = '' #sets command to an empty variable
started = False #sets started to False by default
while True: #starts up an infinite loop (runs until break)
    command = input('> ').lower() #Python is case sensitive, we can declare '.lower()' just once
    if command == 'start': 
        if started: #if the value of started is True, tell the user that
                    #the car is already started, so it doesn't make sense
                    #to start the car again.
            print('Car already started!')
        else: #if the value of started is False, then you can now start 
              #the car, so we set the value of started to True and start the car.
            started = True
            print('Car started')
    elif command == 'stop': 
        if not started: #if the value of started is False, tell the user the car
                        #is already stopped, so we can't stop it again.
            print('Car already stopped!')
        else: #if the value of started is true, we can stop the car, so we set it
              #back to False and output that the car is now stopped.
            started = False
            print('Car stopped')
    elif command == 'help':
        print("""
start - start the car
stop - stop the car
help - show guide
exit - exit the game
""")
    elif command == 'exit':
        break #exits the loop

Summarized: If the car is started, or start = True , then we can not start the car again. Thus, if it is not started, or if start = False , we can now start the car by setting start = True and telling the user the car is started. The same logic applies for stopping the car.

Please let me know if you need any further help or clarification!

EDIT:

First, I just want to clarify: if started: and if started == True are the same thing. Both if statements will only continue if started holds the value of True .

if started:
    print('Car already started!`)

This would be the exact same as:

if started == True:
    print('Car already started!`)

If started had the value of False , neither if statement would be run.

Similarly,

if not started:
    print('Car already stopped`)

is the same as:

if started == False:
    print('Car already stopped`)

As you already said, when you run the code, started = False. The command variable is is also empty.

Now, when you run the command 'start', Python will check if started == True. It isn't, so the else statement will set started to True.

If you run the command 'start' again, Python will once again check if started == True, which it now is. That's when the code returns 'Car already started!'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM