简体   繁体   中英

Execution of while loop problem in Python

I want the user to type "right" or "left" and according to the input, some action will take place.

If the user types "right" the first two times, the smiley becomes sad and can't get out of the forest. If the user types "right" anymore number of times, the smiley always becomes frustrated, chops some trees, makes a table and flips it out as it can't get out of the forest.

As soon as the user types "left", the smiley gets out of the forest.

Here is my Python code:

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while n == "right" or n == "Right" and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while n == "right" or n == "Right" and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")

The problem is that even if the user types "right" third, fourth or fifth time and so on, the "flipping out" action doesn't take place. The smiley only becomes sad, it doesn't come out of the first loop.

What am I doing wrong here?

You're missing brackets in your while statement. The following should give you what you desire:

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n == "right" or n == "Right") and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while (n == "right" or n == "Right") and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")

Your if condition is not working as expected, as the condition is evaluated in order. Therefore, if n=="right" is true, the value of i does not matter. Instead, you should change it to be:

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n == "right" or n == "Right") and i < 3:
    n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    i = i + 1
while (n == "right" or n == "Right") and i >= 3:
    n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("\nYou got out of the Lost Forest!\n\o/")

You don't need multiple while loops but a simple if-elif inside the loop:

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n.lower() == "right"):
    if i < 3:
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    elif i >= 3:
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
    i = i + 1

I'd suggest that you replace n == "right" or n == "Right" with (n.lower() == "right")

That way the user can also input rIght and it won't affect your program.

Also, the reason why your code didn't work was the missing brackets, as you can probably read in the other answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM