[Tutor] Python Tutorial Confusion

Alan Gauld alan.gauld at yahoo.co.uk
Mon Aug 24 04:23:33 EDT 2020


On 24/08/2020 06:08, Ricardo wrote:

>  1  command = ""
>  2  started = False
>  3  while True:

Note that we are entering a loop so this following code
will be executed many times not just once.


>  4      command = input("> ").lower()
>  5      if command == "start":

First time through the car will be NOT started so this command must
start the car.
On subsequent cycles of the code the car may already be started
so we need to check for that. We cannot tell in advance and users
often enter non-sensical commands.(like starting a car that is
already started!)

>  6          if started:
>  7              print("Car is already started!")

So just in case we check here. On the first time through
the loop this will never be executed. On subsequent iterations
it might be.

>  8          else:
>  9              started = True
>  10              print("Car started...")

If the car was NOT started then we must now start it because
that's what the user asked us to do.


>  11      elif command == "stop":
>  12          print("Car stopped.")

But this is probably a fault since as well as printing "stopped"
we should also set started to false to actually stop the car!


>  13      elif command == "help":
>  14          print("""
>  15 start - to start the car
>  16 stop - to stop the car
>  17 quit - to end the game
>  18        """)
>  19      elif command == "quit":
>  20          break

This exits the while loop and therefore the program.

>  21      else:
>  22        print("Sorry, I don't understand that")

>    The person conducting the tutorial says line 2 means the car is NOT
>    started

Wich is correct when we first enter the loop, and only then.
After entering the loop started may get changed to true at
which point the car is started.

>    Line 5 says if command is “start”, it prints “Car Started…” (line10)

It also starts the car by setting started to true. The print
line is not the important one, its the start assignment.
Thinking in terms of a real car the assignment is like turning
on the ignition, the print is like the dashboard light coming on.
The light is only an indication to the user, its turning the
key that actually starts the engine.

But line 5 is only executed if the car is not already started.
As is the case the firt time through the loop.

>    Then, line 6 says “if started” meaning if NOT started (because started =

No it does not mean if not started, it means if started.
This test only makes sense on the second, third, fourth,
etc times through the loop. On the first time it will always fail.

>    False in line2) then print “Car is already started!”) (line7) which I
>    don’t understand. Why would it print “Car is already started!” (line7) if
>    started = false (line2) meaning it is NOT started.

Because line 2 is outside the loop (in what is called the
initialization code) and only executed once. But inside
the loop started can be changed in line 9 (and probably
in the missing line 11a)

>    Then line 9 sets started = True and it just confuses me. 

This line is only executed the first time the user enters
"start" as a command. In future loops the car is already started
so it never gets back here again (unless we add the missing
line 11a)

The main points to understand is that line 2 is executed only
once, then everything under the while statement gets executed
many times - until the user types "quit". And the state of
started can change on each loop around the code, it does not
stay the same.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list