1

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?

1
  • 1
    How's the operator precedence in Python? You write A or B and C, does this mean (A or B) and C or A or (B and C)? Commented Feb 12, 2019 at 14:00

4 Answers 4

2

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/")
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome :) See FlyingTeller's answer for why the brackets matter.
2

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

1 Comment

Thanks for the explanation!!
0

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.

1 Comment

How should I edit my program to prompt user if neither "right" nor "left" is entered without terminating the program?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.