2

I am creating a car game. There are only "start", "stop", "quit" commands. Any other command is not recognized.

command = ""

while command != "quit":
    command = input("Command: ")
    if command == "start":
        print("Car ready to go")
    elif command == "stop":
        print("Car stopped")
    else:
        print("I don't understand that")
else:
    print("Game exited")

All commands work fine except "quit". With the while loop, it causes both the else statements to be executed and prints:

I don't understand that
Game exited

The command = "quit" should render the while condition False and thus skip ahead to execute only the outer else statement. Why is it executing both else statements even thought the while condition is not met?

4
  • 1
    Your interpretation is false. You enter into the loop, you ask for user input, you check it against certain value. Quit hits else block then it loops again et quits the loop and hits the while else. Your program does exactly what you asked it to do. Commented May 18, 2021 at 6:47
  • Also read about while .. else structure. It is not what you think it is. In this case you don't need the second else because you don't have any break statements within the while loop. Commented May 18, 2021 at 6:48
  • 1
    Oh I see where I went wrong. I assumed the while condition is "brought along" throughout the entire while block, so I wrongly interpreted the inner else statement condition as: command != "start" and command != "stop" and command != "quit". So to put it correctly, Python logic flows line by line? Commented May 18, 2021 at 8:29
  • on your conditional logic add a break and it will pass control out of the loop Commented Jun 15, 2021 at 20:27

7 Answers 7

1

while-else works the following way: If the while condition is not satisfied, then the else is executed.
So, when you type "quit" in the program, then if command == "start": and elif command == "stop": conditions are not satisfied.
So, the else is executed, which prints I don't understand that.

Now, again the condition for while loop is checked : command != "quit"
But, this condition is False, as the value of command is now "quit".

So,

else:
    print("Game exited")

is executed, and thus your output becomes

I don't understand that
Game exited
Sign up to request clarification or add additional context in comments.

Comments

1
while command != "quit":
    command = input("Command: ")
    if command == "start":
        print("Car ready to go")
    elif command == "stop":
        print("Car stopped")
    else:
        print("I don't understand that")
else:
    print("Game exited")

Looking at your code, you get input "quit", then it comes down to first if(not true)->next elif(not true)->next else(true)->print("car stopped")->next while(not true)->finish loop and go to "i don't understand that"

Change the code like this:

while command != "quit":
    command = input("Command: ")
    if command == "start":
        print("Car ready to go")
    elif command == "stop":
        print("Car stopped")
    elif command != "quit":
        print("I don't understand that")
else:
    print("Game exited")

1 Comment

To clarify, you mentioned: first if(not true)->next elif(not true)->next else(true)->print("car stopped")->next while(not true)->finish loop and go to "i don't understand that" ********* Should it be: first if(not true)->next elif(not true)->next else(true)->print("i don't understand that")->next while(not true)->finish loop and go to "game exited"?
0

Python programs run line by line. A while loop does not guarantee its condition thoughout the whole indented block. So it's not equivalent to "as soon as [condition] is not met, jump out of the while block"

Your input() is inside the while loop. So it will execute until the end of the while loop first, until it reaches the top of the while loop and checks the condition of the while loop again.

A typical workaround is to have a

while True:
    command = input("Command: ")
    if command == "quit":
        break
    ...

Comments

0

You get the input at the top of the loop, so it completes a full loop iteration before the loop conditional is checked.

while command != "quit":
    command = input("Command: ")
    ...

One option is to move the input fetch to the end of the loop so the loop conditional is the next thing to be checked. You could also break out of the loop early by checking for quit after getting the input with a break.

Comments

0

As Thomas explained if you really want to print the outer statement just remove the else part and it will print the outer part when the command "quit" is given.

Comments

0

Because the command string is assigned after the while condition, so the while condition receives the string 'quit' only after the else statement inside the while loop, which prints I don't understand that.

Comments

0

You are taking input inside the loop. So, it will execute the loop and then check if the condition meets i.e. command != quit.

You can try this code instead.

command = ""

while command != "quit":
    command = input("Command: ")
    if command == "start":
        print("Car ready to go")
    elif command == "stop":
        print("Car stopped")
    elif command != "quit":
        print("I don't understand that")
else:
    print("Game exited")

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.