Reputation: 1
I am a beginner in Python and was watching a 6 hr video by Mosh Hamedami. He has shown an example of designing an elementary Car Game using a while loop. The code is suggested is below:
command = ''
started = False
while command != 'quit':
command = input('Enter a Command: ').lower()
if command == 'start':
if started:
print("Car already Started! Let's go!")
else:
started = True
print("Car Started.... Ready to Go!!!")
elif command == 'stop':
if not started:
print('Car already stopped!')
else:
started = False
print('Car stopped!')
elif command == 'help':
print(" 'start' - to start the car\n'stop' - to stop the car\n'quit' - to quit/close the game.")
elif command == 'quit':
break
else:
print('Sorry, I do not understand that!')
The above program runs perfectly, But if we exclude the elif command == 'quit' block of code from the above program, and give the User-input: 'quit' the program returns the Output: Sorry, I do not understand that! But according to my understanding of the while loop, when: User-input: 'quit' The while loop should stop getting executed since while condition becomes False.
Now, if while loop stops executing with user-input "quit" then how the else block defined within the while condition is getting executed?
Upvotes: 0
Views: 2588
Reputation: 655
Statement are executed sequentially.
The condition of the while statement is executed at the beginning of the loop while the "else:" is executed near the end. After the "input" is executed, the "else" near end of loop is executed first.
Upvotes: 0
Reputation: 531035
A while
loop does not terminate when the condition becomes false. It terminates when it evaluates the condition and the condition is found to be false. That evaluation doesn't happen until the beginning of each loop iteration, not immediately after some event occurs that will allow the condition to become false.
A better way to write this loop is to simply use True
as the condition, as it doesn't require you to initialize command
to a known non-terminating value, then let a break
statement somewhere the loop terminate the command when appropriate.
started = False
while True:
command = input('Enter a Command: ').lower()
if command == 'quit':
break
if command == 'start':
if started:
print("Car already Started! Let's go!")
else:
started = True
print("Car Started.... Ready to Go!!!")
elif command == 'stop':
if not started:
print('Car already stopped!')
else:
started = False
print('Car stopped!')
elif command == 'help':
print(" 'start' - to start the car\n'stop' - to stop the car\n'quit' - to quit/close the game.")
else:
print('Sorry, I do not understand that!')
Of course, you don't need two separate if
statements as I've shown here; you could combine them into one with if command == 'start'
being the first elif
clause of the combined if
statement. But this provides an explicit boundary between "code that terminates the loop" and "code that allows the loop to continue".
Upvotes: 2
Reputation: 328
The program actually stops when you type quit
, but before stopping it prints "Sorry, I do not understand that!". You can fix this by putting command = input('Enter a Command: ').lower()
before while
and in the end of the while
like this (so that while
will check if command != quit
immediately after inputing):
command = ''
started = False
command = input('Enter a Command: ').lower()
while command != 'quit':
if command == 'start':
if started:
print("Car already Started! Let's go!")
else:
started = True
print("Car Started.... Ready to Go!!!")
elif command == 'stop':
if not started:
print('Car already stopped!')
else:
started = False
print('Car stopped!')
elif command == 'help':
print(" 'start' - to start the car\n'stop' - to stop the car\n'quit' - to quit/close the game.")
else:
print('Sorry, I do not understand that!')
command = input('Enter a Command: ').lower()
Upvotes: 1