Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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?

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    Thanks for the explanation!! Commented Feb 12, 2019 at 14:04

lang-py

AltStyle によって変換されたページ (->オリジナル) /