bigtark
bigtark

Reputation: 19

Having trouble with python inputs

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")

Upvotes: -1

Views: 285

Answers (4)

OakenDuck
OakenDuck

Reputation: 483

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.

Upvotes: -1

sal
sal

Reputation: 3593

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

Upvotes: -1

Oliver Ni
Oliver Ni

Reputation: 2664

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

Upvotes: 3

Haytek
Haytek

Reputation: 101

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

Upvotes: -1

Related Questions