Nov-17-2019, 10:57 PM
(Nov-17-2019, 08:13 PM)Gribouillis Wrote: You could rename thestarted
variable toalready_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?