while True:
user_input = input('> ').lower()
if user_input == 'help':
print("""start - to start the car
stop - to stop the car
quit - to exit
""")
elif user_input == 'start':
print('car started...Ready to go!')
elif user_input == 'stop':
print('car stopped...')
elif user_input == 'quit':
break
else:
print('I donn\'t understand that')
So this is a simple program. However, the additional task was: from the code above we can understand if the user entered "start" a message will be printed on the screen saying "car started" and if the user enters the same thing again, the same message will keep appearing. however, the task was if the user entered "start" the second time it should appear a different message, For example "the car is already started".
So only the first time it should show "car started" and rest of the times it should show "car is already started". the code below will show the answer but I cannot understand the logic behind it.
started = False
while True:
user_input = input('> ').lower()
if user_input == 'help':
print("""
start - to start the car
stop - to stop the car
quit - to exit
""")
elif user_input == 'start':
if started:
print("car is already started!")
else:
started = True
print('car started...')
elif user_input == 'stop':
if not started:
print("car is already stopped")
else:
started = False
print('car stopped...')
elif user_input == 'quit':
break
else:
print('I donn\'t understand that')
I do understand the while loop well but I don't understand the logic behind setting things to false and then true. How is the code being executed? How's the loop and if statements handling them?
code reference https://www.youtube.com/watch?v=_uQrJ0TkZlc&ab_channel=ProgrammingwithMosh while loops part
CodePudding user response:
I try to go step by step, look at the comments.
''' Set the variable started to False, this will indicate if the car is started or not.
At the beginning it should be False, since we haven't started it. '''
started = False
''' While True means, we stuck inside this while loop FORVERER, so we will have to
'break' out of it using the break statement. '''
while True:
# Get the user input, and convert to lower case
user_input = input('> ').lower()
if user_input == 'help':
''' If user input was 'help', print the help, and then jump to the begining of the
while loop. '''
print("""
start - to start the car
stop - to stop the car
quit - to exit
""")
elif user_input == 'start':
''' If the input was 'start', we go into this branch '''
if started:
''' If started is True (note, initially it was false), we already started the car.
Just print, and start the loop again. '''
print("car is already started!")
else:
''' Else (started is False), we set it to True, indicating that we started the car,
and print that we started it. Then start the loop again. '''
started = True
print('car started...')
elif user_input == 'stop':
''' The input is 'stop', so we go into this branch '''
if not started: # Equivalent to 'if started == False'
''' We try to stop the car, but started is already false, so we can't stop twice. '''
print("car is already stopped")
else:
''' If started wasn't false, than it is true, and this is a logical crap.
BUT, we are here since started was True, so we set it to False, and print that
we stopped the car. '''
started = False
print('car stopped...')
elif user_input == 'quit':
''' User input was quit, so this is our exit point from this infinite 'while True' loop. '''
break
else:
''' We had no idea what was the input, print this message, and start the loop again. '''
print('I donn\'t understand that')
CodePudding user response:
So Marz,
Your first program has only one set of check around user input ie if else works only based on user input. To take into consideration the state of car ie is it started or stopped we need another variable. So comes 'started' variable into picture. In your second program code the message is displayed checking both - user input and condition/state of car. Hope that makes sense.