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
#2
Hi New_coder,
The best way to find out if it works: just run the code and see if it does what you want. If you don't understand why it does not work: just ask us in this forum.

A few things I would like to say:
input = ("")
NEVER USE THE NAME OF A KEYWORD OR A BUILT-IN AS A NAME OF A VARIABLE! After that line you will not be able to use the input() function anymore.
I understand the code should repeat asking commands. I know only 2 ways to repeat code blocks: while and for. I don't see an elegant way to get the same result in an other way.
There are more problems in your code, but please just run it and you will find them yourself. It is the best way to learn.
Reply
#3
Thanks ibreeden. I've revised my code as follows below but am now getting an error message that the names 'start' and 'stop' are not defined.

user_input = input("")
if user_input.lower() == help:
     print("start - to start the car")
     print("stop - to stop the car")
     print("quit - to quit")
if user_input.lower() == start:
     print("Car started...")
if user_input.lower() == stop:
     print("Car stopped.")
if user_input.lower() == quit:
     print("Game is over")
else:
     print("Sorry, I don't understand that")
Below is what I'm trying to do in plain English; my question is why this can't be done with if statements rather than with while and for loops:

1 ) Take the user's input (as a string)
2 ) If the user's input in lowercase is "help," print the three lines beginning with "start - to start the car"
3 ) If the user's input in lowercase is "start," print "Car started..."
4 ) If the user's input in lowercase is "stop," print "Car stopped."
5 ) If the user's input in lowercase is "quit," print "Game is over"
6 ) If the user's input is anything other than "help," "start," "stop," or "quit," print "Sorry, I don't understand that"

Thanks again in advance.
Reply
#4
(Dec-12-2021, 10:14 PM)new_coder_231013 Wrote: getting an error message that the names 'start' and 'stop' are not defined
That is right. You have to put string literals like "start" and "stop" in quotes, like you had in the first version.

(Dec-12-2021, 10:14 PM)new_coder_231013 Wrote: my question is why this can't be done with if statements rather than with while and for loops
No, if is for a choice, while is to repeat a block.

Very nice what you've got so far. It should work. You might want to experiment with elif. And instead of repeating user_input.lower() 4 times, you might do it once and assign this to another variable, e.g. "lower_input".
Reply
#5
new_coder_231013 Wrote:my question is why this can't be done with if statements rather than with while and for loops
The sequence that you describe can be done with ifs only, but the solution does more than that: unless the user types "quit", the program keeps waiting for new commands from the user. If you want to read a single command and react to it, you don't need a loop, but if you want to process a sequence of commands, you need one.
Reply
#6
Also, you have asked for input("") several times. You did not just get the input and store it.
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
What this says is, without giving a prompt, read a string from the console. Then, discard this string and read another string from the console. If this is equal to the word "help", print out the help message. Then ask for another input string. If this is not equal to "help" (even if it is "start", "stop" or "quit"), say that you don't understand it. Then, get another string. If this third input is "start", start the car, then read another string from the console. If this is "stop", stop; iif it is "bugsbunny", ignore it without issuing any warning message, then read another string from the console. If this is "quit" then quit; otherwise do nothing.

Since there is no loop, the program always terminates, unless you type "quit", in which case it should complain that there is no loop for the break statement. Overall, a terrible piece of code. It does not follow the specification of what it should do. It keeps asking for strings.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  quiz game - problem with loading statements from file lapiduch 2 1,074 Apr-20-2023, 06:13 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 1,959 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  RockPaperScissor program while loop malfunction tonyliang19 3 2,874 Apr-03-2020, 11:09 PM
Last Post: SheeppOSU
  Simple maze game controls OmegaSupreme 4 4,036 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,679 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,576 May-17-2018, 05:39 PM
Last Post: j.crater
  Supposedly simple program not working! Please help! BenjaminDJ 5 4,060 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,225 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  help with while loop on dice game sean5 2 4,190 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