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


Messages In This Thread
Confusing logic - by Blob - Nov-17-2019, 07:41 PM
RE: Confusing logic - by Gribouillis - Nov-17-2019, 08:13 PM
RE: Confusing logic - by Blob - Nov-17-2019, 10:57 PM
RE: Confusing logic - by Gribouillis - Nov-17-2019, 11:27 PM
RE: Confusing logic - by Blob - Nov-18-2019, 03:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Confusing in [for loop] topic Sherine 11 3,620 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,853 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,367 May-16-2018, 05:05 PM
Last Post: Larz60+
  Confusing Math DrJu 2 3,274 Jan-18-2018, 10:47 PM
Last Post: Windspar
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,806 Sep-03-2017, 05:00 PM
Last Post: hbknjr
  CPU utilisation is confusing Bidgey225 6 5,218 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