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?

toothpick
  • 125
  • 1
  • 7
  • 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. – jlandercy May 18 '21 at 06: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. – Selcuk May 18 '21 at 06: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? – toothpick May 18 '21 at 08:29
  • on your conditional logic add a break and it will pass control out of the loop – Golden Lion Jun 15 '21 at 20:27

7 Answers7

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
CoolCoder
  • 786
  • 7
  • 20
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")
pullidea-dev
  • 1,768
  • 1
  • 7
  • 23
  • 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**"_? – toothpick May 18 '21 at 08:54
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
    ...
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
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.

Stewart Smith
  • 1,396
  • 13
  • 28
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.

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.

Kevin Gao
  • 85
  • 6
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")