1

Objective:

  • If the user types "right" the first two times, the smiley becomes sad as it can't get out of the forest.

  • If the user types "right" 3rd, 4th, 5th time an so on, the smiley becomes frustrated, chops some trees, makes a table and flips it over.

  • If the user types anything else, a message showing "Invalid input" is shown to the user and asked to give input again.

  • If the user types "left", the smiley gets out of the forest and the program terminates.

Python code:

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    
while (n.lower() != "right") and (n.lower() != "left"):
    n = input("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")    
while (n.lower() == "left"):
    print("\nYou got out of the Lost Forest!\n\o/")
    break

Bug:

If the user types anything other than "right" or "left" for the first two times and then types "right", the program immediately terminates without giving chance to type "left".

How should I edit my code?

6
  • 2
    you probably want to wrap your entire code with a loop that returns back to the top (first while loop, that is). That loop can for example have a while(playing==True): and before the break on your last loop, set playing=False Commented Feb 13, 2019 at 2:51
  • What have you tried so far? Commented Feb 13, 2019 at 2:55
  • @AlexYu I tried changing my entire code to if - else format, but still unsuccessful. Commented Feb 13, 2019 at 2:59
  • I suppose you need to wrap everything into while True loop as @Tacratis suggests Commented Feb 13, 2019 at 3:02
  • @AlexYu What is while True? I am not familiar with it. Commented Feb 13, 2019 at 3:05

2 Answers 2

1

More like this:

lost = "You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? "
inp = lost
rightCount = 0
while True:
    n = input(inp)
    if (n.lower() == "right"):
        rightCount = rightCount + 1
        if rightCount > 3:
            inp = ("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
        else:
           inp = lost  
    elif n.lower() == "left":
        print("\nYou got out of the Lost Forest!\n\o/")    #syntax error fixed
        break
    else:
        inp = ("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")  
Sign up to request clarification or add additional context in comments.

Comments

0

I suppose that you forgot to wrap everything into while True loop (or other kind of endless loop).

Is it what you need?

while True:
    n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
    i = 1
    if (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    
    if (n.lower() != "right") and (n.lower() != "left"):
        n = input("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")    
    if (n.lower() == "left"):
        print("\nYou got out of the Lost Forest!\n\o/")
        break

2 Comments

There are some bugs in your code. If I type "car" first time, it says "Invalid Input" but if I type "car" second time it doesn't say "Invalid Input". One more bug, even after typing "right" more than two times, the smiley never flips the table, it only shows a sad face.
Those are your bugs in your code. I did not try solve all your needs, I solved only what you marked as a problem - what is below "Bug:" in your question

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.