-1

I'm fairly new to programming and found an exercise that has you make a game. Basically, the program asks for 3 inputs: stop, start, and exit. Start makes the car start, stop makes the car stop, and exit is self explanatory. However, if you input start when the car is already started, it'll tell you the car is started, and so on. However, when I put an input into the terminal, nothing shows up. Can anybody tell me what I'm doing wrong?

Here's my code:

started = False carstate = str(input()) while carstate != "exit": if carstate == "start": if started == False: started == True print("Car has started... ready to go!") if started == True: print("Car has already started") elif carstate == "stop": if started == False: print("Car is already stopped!") if started == True: started == False print("Car is stopped") if carstate == "exit": sys.exit() if carstate == "help": print("Start - starts the car") print("Stop - stops the car") print("Exit - exits the game") 
4
  • 2
    I believe you mean to get the input again in the while loop Commented Feb 9, 2020 at 21:28
  • you do not require the inner if statement after checking for carstate == "start": remove the if started == 'False':... Commented Feb 9, 2020 at 21:32
  • Btw, you don't need str(input()), since input() already returns a string. (You only need to convert type if you're trying to convert the input to int, or float, or some other non-string type) Commented Feb 9, 2020 at 21:40
  • ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Feb 10, 2020 at 1:36

4 Answers 4

3

Welcome to StackOverflow!

The first issue here is, since carstate = input() is not inside the while loop, your program only calls for the user input once. To ask for the user input multiple times, you should put it inside like this:

carstate = "" while carstate != "exit": carstate = input() # ... 

So, the program will continue to ask the user for input.

The second issue is the difference between = and ==. = is used when you want to assign a value to a variable, while == is used when you want to compare something to a variable. So, when you say started == True, you should really be using =.

In addition, you should place the help block also inside the while loop, otherwise it will never get executed.

So the final code should be something like:

import sys started = False carstate = "" while carstate != "exit": carstate = input() if carstate == "start": if started == False: started = True print("Car has started... ready to go!") elif started == True: print("Car has already started") elif carstate == "stop": if started == False: print("Car is already stopped!") elif started == True: started = False print("Car is stopped") elif carstate == "help": print("Start - starts the car") print("Stop - stops the car") print("Exit - exits the game") if carstate == "exit": sys.exit() 

Working example

Sign up to request clarification or add additional context in comments.

2 Comments

why str(input()) when input() returns a string?
Glad I could help! If you don't mind, could you please click the checkmark next to the answer to mark it as accepted?
-1

You ask for user input at the beginning, and then do the while loop. As you don't ask again for a different action in the while, it loops with "Car as started... ready to go!". You must do another "input()" inside the loop

Comments

-1

Your current code gets likely stuck inside the while loop. If you enter the loop by inputting start you will not get anything printed, nor any chance to change that.

Another note goes for the two additional state checks for exit and help. I would include those (or at least the help one) inside the main while loop.

And in two instances I am sure you wanted to use the assignment = rather than the equality check ==. And some other cleanup :-)

Something like this should do:

started = False carstate = "" # initialize empty, we will ask for input at every iteration while carstate != "exit": carstate = input() # ask what we want to do next if carstate == "start": if started == False: started = True # here you want to make an assignment (use '=' not '==') print("Car has started... ready to go!") elif started == True: # change to "elif", so it avoids double prints print("Car has already started") elif carstate == "stop": if started == False: print("Car is already stopped!") elif started == True: # change to "elif", so it avoids double prints started = False # here you want to make an assignment (use '=' not '==') print("Car is stopped") elif carstate == "help": print("Start - starts the car") print("Stop - stops the car") print("Exit - exits the game") if carstate == "exit": sys.exit() 

Might be able to try it here: https://onlinegdb.com/S1gJ6lRGL

Comments

-1

When you first start the program, you only ask for input once, when you first ask for carstate. If it doesn't redefine carstate on every iteration, carstate = input(), then it will only keep the original initialization of it.

Also, even when it does, the part where it checks if started == True and then sets it to True if it is, you're comparing == instead of redefining =. This means it will never set it to True.

Another thing is that when you ask if it's False, the next if should be an elif, otherwise it'll trip both the first time around. An example, using the only ifs and not an elif:

>>> start Car has started... ready to go! Car has already started >>> start Car has already started 

Using the elif:

>>> start Car has started... ready to go! >>> start Car has already started 

You'll also need to import sys at the top, so it won't return a name error when it tries to run sys.exit().

import sys started = False carstate = "" while carstate != "exit": carstate = input() # asking for input each iteration. if carstate == "start": if started == False: started = True # we're setting it, not comparing it. print("Car has started... ready to go!") elif started == True: # remember the elif print("Car has already started") elif carstate == "stop": if started == False: print("Car is already stopped!") elif started == True: started = False print("Car is stopped") if carstate == "help": print("Start - starts the car") print("Stop - stops the car") print("Exit - exits the game") if carstate == "exit": sys.exit() 

Hope that helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.