Python Forum
Using If Statements Instead of While Loop in Simple Game Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using If Statements Instead of While Loop in Simple Game Program
#1
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:

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
Reply


Messages In This Thread
Using If Statements Instead of While Loop in Simple Game Program - by new_coder_231013 - Dec-12-2021, 04:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  quiz game - problem with loading statements from file lapiduch 2 1,151 Apr-20-2023, 06:13 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 2,029 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  RockPaperScissor program while loop malfunction tonyliang19 3 2,946 Apr-03-2020, 11:09 PM
Last Post: SheeppOSU
  Simple maze game controls OmegaSupreme 4 4,160 Apr-26-2019, 08:55 AM
Last Post: OmegaSupreme
  Program that, inside a loop, does multiple things. needs to print in a certain way reidmcleod 1 2,737 Feb-19-2019, 02:35 PM
Last Post: marienbad
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,649 May-17-2018, 05:39 PM
Last Post: j.crater
  Supposedly simple program not working! Please help! BenjaminDJ 5 4,158 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,304 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  help with while loop on dice game sean5 2 4,270 Dec-14-2017, 07:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020