At run time
The code behaves in an unusual way .when I say start it shows me car started ready to go But when I say start again it till shows the same output I want the code to show me car already started when I say start for the second time
command = ""
while command != "quit":
unit = input("> ").lower()
if unit == "start":
print ("car started ... ready to go")
elif unit == "stop":
print("car stopped")
elif unit == "help":
print("""
start - start the car
stop - stop the car
quit - exit
""")
elif unit == "quit":
break
else:
print("sorry I don't understand")
CodePudding user response:
You will need some state that stores in which state the car currently is in order to make this happen. In your case you could simply use a flag car_running
which you set accordingly.
car_running = False
command = ""
while command != "quit":
unit = input("> ").lower()
if unit == "start":
if car_running:
print("car has already been started")
else:
print("car started ... ready to go")
car_running = True
elif unit == "stop":
if car_running:
print("car stopped")
car_running = False
else:
print("car is not running")
elif unit == "help":
print(""" start - start the car stop - stop the car quit - exit """)
elif unit == "quit":
break
else:
print("sorry I don't understand")
If you have multiple states are car can be in you could use an enum.
What your building is essentially a finite-state machine.