Dec-12-2021, 04:04 PM
Hello,
I'm going through an online Python tutorial and there's an exercise that involves creating a "car game" program that does the following:
1 ) If the user types "help" (uppercase or lowercase), the program prints the following text on three separate lines:
start - to start the car
stop - to stop the car
quit - to quit
2 ) If the user types "start" (uppercase or lowercase), the program prints "Car started..."
3 ) If the user types "stop" (uppercase or lowercase), the program prints "Car stopped."
4 ) If the user types "quit" (uppercase or lowercase), the program terminates
5 ) If the user types anything else, the program prints "Sorry, I don't understand that"
Below is the code that is presented in the tutorial as the solution for this exercise:
I'm going through an online Python tutorial and there's an exercise that involves creating a "car game" program that does the following:
1 ) If the user types "help" (uppercase or lowercase), the program prints the following text on three separate lines:
start - to start the car
stop - to stop the car
quit - to quit
2 ) If the user types "start" (uppercase or lowercase), the program prints "Car started..."
3 ) If the user types "stop" (uppercase or lowercase), the program prints "Car stopped."
4 ) If the user types "quit" (uppercase or lowercase), the program terminates
5 ) If the user types anything else, the program prints "Sorry, I don't understand that"
Below is the code that is presented in the tutorial as the solution for this exercise:
command = ""
while True:
command = input("> ").lower()
if command == "start":
print("Car started...")
elif command == "stop":
print("Car stopped.")
elif command == "help":
print("""
start - to start the car
stop - to stop the car
quit - to quit
""")
elif command == "quit":
break
else:
print("Sorry, I don't understand that")Below is the code that I came up with for this exercise. I realize it's not the most elegant or efficient way to build the "car game" but I'm wondering if there is a way to make it work (for my own learning and since it was the first thing that came to mind when I tried to do the exercise). Any help or insight would be greatly appreciated. Thanks.input = ("")
if input("").lower() == "help":
print("start - to start the car")
print("stop - to stop the car")
print("quit - to quit")
if input("").lower() != "help"
print("Sorry, I don't understand that")
if input("").lower() == "start":
print("Car started...")
if input("").lower() == "stop":
print("Car stopped.")
if input("").lower() == "quit":
break
