Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusing logic
#1
OK, so this is from my tutorial and it works fine, but I'm confused by why, so I'm missing something...

command = ""
started = False

while True:
    command = input("> ").lower()
    if command == "start":
        if started:
            print("Car already started.")
        else:
            started = True
            print("Car started.")
    elif command == "stop":
        print("Car stopped.")
    elif command == "help":
        print("""
start - to start car
stop - to stop car
quit - to quit game
        """)
    elif command == "quit":
        break
    else:
        print("Sorry, I don't understand that.")
I don't understand why it works this way, I thought this would work if I set the variable "started" to True, not False. It is the exact opposite to what I expected.

if command == "start":
        if started:
            print("Car already started.")
        else:
            started = True
            print("Car started.")
So, someone inputs "start" (the car), if started (false, so not started), print that it is "already started"... ugh it's giving me a headache Dodgy
Reply
#2
You could rename the started variable to already_started if it makes the code clearer. Also in the 'stop' case, this variable should be set to False, and an error could be written such as 'car already stopped' if the variable is false.
Reply
#3
(Nov-17-2019, 08:13 PM)Gribouillis Wrote: You could rename the started variable to already_started if it makes the code clearer. Also in the 'stop' case, this variable should be set to False, and an error could be written such as 'car already stopped' if the variable is false.

Thanks for your reply Gribouillis.

command = ""
already_started = False

while True:
    command = input("> ").lower()
    if command == "start":
        if already_started:
            print("Car already started.")
        else:
            already_started = True
            print("Car started.")
    elif command == "stop":
        if not already_started:
            print("Car is already stopped")
        else:
            already_started = False
            print("Car stopped.")
This is with the stop function. I just wrote a long message to you, and as I was doing so I think something might have clicked with me, so this is a completely different message and maybe I can just check my understanding now.

I think I was thinking about how the boolean works in completely the wrong way. I was viewing it as essentially the representation of the car/its engine. So True, the engine is on, False it is off, though in the above example, it would be the vice versa.

Please bear with me and tell me if now I'm thinking in the right way, I'll try not to ramble if I can.

So now, I think, it's more like already_started = False is that this piece of code is turned off, so the first time through the loop the if already_started:code is set to False, meaning it doesn't work/print "Car already started.". But as it moves to the only other answer, "else", I turn on that piece of code/set it to True. Then, as it loops around the second time, when I get to if already_started: it does execute the code, and prints "already started". Then because now it is set to True, each time through the loop, it will execute if already_started: print("Car already started.").

Now if someone inputs "stop".

if not already_started:, if the value of the already_started variable hasn't been set to True/turned on, then print("Car is already stopped"), because no one has entered "start" to start the car (and therefore changed the variable to True). Else: already_started = False, I set the variable to False/turn off that piece of code, and print("Car stopped.")

Does that sum it up OK?
Reply
#4
Blob Wrote:Does that sum it up OK?
Yes I think it does. Already_started is an example of a state variable that describes the state of a system.
Reply
#5
Excellent, thank you Gribouillis.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Confusing in [for loop] topic Sherine 11 3,376 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,777 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,302 May-16-2018, 05:05 PM
Last Post: Larz60+
  Confusing Math DrJu 2 3,209 Jan-18-2018, 10:47 PM
Last Post: Windspar
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,746 Sep-03-2017, 05:00 PM
Last Post: hbknjr
  CPU utilisation is confusing Bidgey225 6 5,099 Mar-20-2017, 01:49 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020