Issue
So I've literally just begun learning python, starting with some lengthy youtube tutorials. One tutorial wanted me to create a sim/game of starting and stopping a car. My code is very similar to the solution. I'm not sure what the problem is. The specific issue is the program fails to return the print function for when the car is already started or stopped--it just reiterates the start/stop print as if the boolean code to indicate whether the car is on or off at present doesn't exist.
start = input('Enter "help" to see instructions: ').lower()
if start == 'help':
started = False
user_input = input("start - start the car\nstop - stop the car\nquit - exist program\n").lower()
while True:
if user_input == "start":
if started:
print("The car is already on.\n") #so if i enter start two times in a row, the second time it should print "The car is already on."
else:
started = True
print("The car has started. Now what?\n")
elif user_input == "stop":
if not started:
print("The car is already stopped.\n")
else:
started = False
print("The car has stopped. Now what?\n") #if this is the first command entered after "help", it should be printing "The car is already stopped." but doesn't do that.
elif user_input == 'quit':
break
else:
print("This is not a valid command.")
Solution
You need to keep taking user_input inside the while loop. Just move user_input inside the while loop.
start = input('Enter "help" to see instructions: ').lower()
if start == 'help':
started = False
while True:
user_input = input("start - start the car\nstop - stop the car\nquit - exist program\n").lower()
if user_input == "start":
if started:
print("The car is already on.\n") #so if i enter start two times in a row, the second time it should print "The car is already on."
else:
started = True
print("The car has started. Now what?\n")
elif user_input == "stop":
if not started:
print("The car is already stopped.\n")
else:
started = False
print("The car has stopped. Now what?\n") #if this is the first command entered after "help", it should be printing "The car is already stopped." but doesn't do that.
elif user_input == 'quit':
break
else:
print("This is not a valid command.")
And here is the output:
[evaluate untitlewerwd-1.py]
Enter "help" to see instructions: help
start - start the car
stop - stop the car
quit - exist program
start
The car has started. Now what?
start - start the car
stop - stop the car
quit - exist program
start
The car is already on.
start - start the car
stop - stop the car
quit - exist program
stop
The car has stopped. Now what?
start - start the car
stop - stop the car
quit - exist program
stop
The car is already stopped.
start - start the car
stop - stop the car
quit - exist program
stop
The car is already stopped.
start - start the car
stop - stop the car
quit - exist program
quit
Answered By - Frank
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.