command = input("> ")
started = False
while True:
if command == "start":
if started:
print("car has already started")
else:
started = True
print("car started")
elif command == "stop":
if not started:
print("car has already stopped")
else:
started = False
print("car is stopped")
elif command == "quit":
break
it renders "car has already started" infinite time when i state "start" and likewise with other inputs. how do i inhibit this? would be very helpful if you explain it in beginner terms, thank you!!
Male sure to add the input to thie while loop. In your case, it is just taking one input and then the value of command
is fixed. So, it is an infinite loop.
But when you change the location and keep it inside the loop, every time it asks for the input, the value of command
will change.
Also, .lower()
will convert the input to lower case to remove case insensitivity
started = False
while True:
command=input('>> ').lower()
if command == "start":
if started:
print("car has already started")
else:
started = True
print("car started")
elif command == "stop":
if not started:
print("car has already stopped")
else:
started = False
print("car is stopped")
elif command == "quit":
break
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments