Python Forum
Confusing logic - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Confusing logic (/thread-22555.html)



Confusing logic - Blob - Nov-17-2019

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


RE: Confusing logic - Gribouillis - Nov-17-2019

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.


RE: Confusing logic - Blob - Nov-17-2019

(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?


RE: Confusing logic - Gribouillis - Nov-17-2019

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.


RE: Confusing logic - Blob - Nov-18-2019

Excellent, thank you Gribouillis.