In: Computer Science
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks!
# Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it should display: ">" , then the terminal waits for user input # If we type "help", the terminal will list the commands that our program/game currently supports # start - to start the car # stop - to stop the car # quit - to terminate the game # If we type anything other than the currently supported commands, # then the program should display: "Sorry, I don't understand." # If we type start, then display: "Car started... ready to go!" # If we type start while car is already started, then display: "Car already started." # If we type stop, then display: "Car stopped." # If we type stop, when car is already stopped, display: "Car already stopped." # If we type quit, then terminate the game. # my attempt for this program: print("This program simulates a car game.") print('Type "help" to display a list of working commands.') help = ''' List of working commands: "start": start the car "stop": stops the car "quit": terminates the game ''' command = "" #initializes as an empty str variable started = False #initialize started to False while True: #means the following block of code in the while loop will be executed till it reaches 'break' function command = input("> ").lower() #.lower method will lowercase any and every letters inputted by user if command == "start": if started: print("Car already started.") else: print("Car started... ready to go!") started == True elif command == "stop": if not started: # implies if car is stopped print("Car already stopped!") else: print("Car has now stopped.") started == False elif command == "help": print(help) elif command == "quit": print("Game terminated.") break else: print("Sorry, I don't understand that. Please enter a working command.")
NOTE -
There was a little mistake in your program which was the reason that was stopping your program from working properly.
While trying to assign True to started in the else part of the start command you used "==" instead of "=". Same thing is repeated when you are trying to assign False to started in the else part of the stop command. "==" is a comparison operator and cannot be used for assigning. "=" is the assignment operator that can be used while assigning a value to a variable.
I have modified the code accordingly and also attached a screenshot with the code.
CODE -
print("This program simulates a car game.")
print('Type "help" to display a list of working commands.')
help = '''
List of working commands:
"start": start the car
"stop": stops the car
"quit": terminates the game
'''
command = "" #initializes as an empty str variable
started = False #initialize started to False
while True: #means the following block of code in the while loop
will be executed till it reaches 'break' function
command = input("> ").lower() #.lower method will lowercase any
and every letters inputted by user
if command == "start":
if started:
print("Car already started.")
else:
print("Car started... ready to go!")
started = True
elif command == "stop":
if not started: # implies if car is stopped
print("Car already stopped!")
else:
print("Car has now stopped.")
started = False
elif command == "help":
print(help)
elif command == "quit":
print("Game terminated.")
break
else:
print("Sorry, I don't understand that. Please enter a working
command.")
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.