Please explain the role of Booleans in this piece of code

Hi guys,

I am a total Python noob and currently I’m following a YouTube course on python. It also contains some basic coding exercises. One of them asks the student to create a simple game, to start a car. It looks like this:

if you type “help” it displays your options:

  • type “start” to start the car
  • type “stop” to stop the car
  • type “quit” to terminate the program.
  • anything else you type it will return “i don’t understand”
    if you type “start” it will display:
    “Car started”
    similar if you type “stop”.
    But if you type start after the car is started it will display “Car is already started.” Similar if you type stop when car is stopped. There comes the role of the Booleans that I don’t quite understand. Here is the code:
command=""
started=False
while command !="quit":
    command=input(">").lower() 
    if command =="start":
        if started:
            print("Car is already started")
        else:
            started=True
            print("Car started")
    elif command=="stop":
        if not started:
            print("Car already stoped")
        else:
            started=False
            print("Car stopped")
    elif command=="help":
        print("""
        start -to start the car
        stop - to stop the car""")
    elif command=="quit":
        break
    else:
        print("I don't understand")

To start with, i don’t get how the boolean started=False works. The author of the video says we need to now if the car is started or not, so we establish a variable caled “started” and set it to False, because the car is not started. I don’t understand how something stored in a variable whether is True or false connects to the input of the user. As I see, the author tries to suggest that the name of the variable - “started” has something to do with the status of the car. But for all i understand the variable might be named as well “mumbo_jumbo” and the code will get executed as well.

Next, i don’t understand how lines 5-7 work:

if command =="start":
        if started:
            print("Car is already started")

It says there that if started, than the return is “Car is already started”. But the value of started is “False”, which means car is not started. To my mind, if I type start, it should be the opposite, should return “Car started”, because started=false, i.e. car is stoped. Similarly that means that in lines 8-10, it should return “car is already started” because started=True, car is started, so if you type start it should return that is already started. Everything is reversed to me and very confusing. I think I should know more on how Booleans work in python, but there tons of documentation on the internet and I would like something relevant for my case. Can you help?

The car is assumed to not be started when the program starts, so it is initialized as False. Remember that this is running under a while loop and will keep looping over and over until quit issued as a command, started will not always be False as it will change depending on whether start or stop are issued as a command.

Once the input is given the command start, the boolean will be set to True if the started is not True, which by default it is not. If you try to issue start twice, it will tell you it is already started as started will be True already.

    if command =="start":
        if started:
            print("Car is already started")
        else:
            started=True
            print("Car started")

From then on, the care is assumed to be running unless stop is issued as a command. If the command stop is issued, and started is True, then started will be set to False again.

    elif command=="stop":
        if not started:
            print("Car already stoped")
        else:
            started=False
            print("Car stopped")

The loop continues until quit is issued.

To answer the first question, you can name the variable “mumbo_jumbo” if you want to, but “started” is a better name because it’ll make more sense to anyone who reads the program.

2 Likes

Thanks. I think i might have got it. You say:

Once the input is given the command start , the boolean will be set to True if the started is not True , which by default it is not. If you try to issue start twice, it will tell you it is already started as started will be True already.

If i understood well, right here:

if command =="start":
        if started:
            print("Car is already started")

started is set to “True” , is no longer false, while here:

else:
            started=True
            print("Car started")

started is as initially set, i.e. false, and turned true by typing “start”

if I understood right the above code sequence are in reversed order to the temporal sequence when the user types start. i guess that is what was confusing me

Sounds like you got it. Code may not always be in one’s logical way of thinking about the flow of events, so it is good to get used to understanding code as it is written. But I get it, it is tricky when you are new to programming.

I do not know if this metaphor will help, but the car is being treated as a “state machine”, with 2 states “started” and “stopped”. The started variable indicates which start the car’s engine is in.

The “start” command requests moving from the stopped state to the started state. The “stop” command moves from started to stopped. A request unsuitable for the current state gets an error message eg “Car is already started” and the state does not change.

Here’s a little diagram which may help to visualise this:

car

The car really only has 2 states, so a Boolean value (which can only be True or False) is enough to represent this, and has the benefit that you can write if started: in your Python code to express a test of the current state in nice English-like language.

The diagram shows 4 states, but the programme doesn’t actually spent any time in the first and last, so a 2-value started variable is enough during the while-loop.

Cheers,
Cameron Simpson

2 Likes