Reputation: 211
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?
Upvotes: 1
Views: 401
Reputation: 16772
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
Upvotes: 0
Reputation: 254
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.
Upvotes: 0
Reputation: 20472
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/")
Upvotes: 2
Reputation: 5686
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/")
Upvotes: 2