0
print('Welcome to the Treasure Hunt!')
print('Your goal is to find the treasure.')
direction = input('You are at a crossroad. Where do you want to go? Type "left" or "right". \n').lower()

if direction == "left":
    wait_or_swim = input('You have come to a lake. There is an island in middle of the lake. Type "wait" to wait for a boat or "swim" to swim accross the lake? \n').lower()
elif direction == "right":
    print('You are crushed by a truck. Game Over!')
else:
    print('You chose an option that doesn\'t exist.')

    
if wait_or_swim == "wait":
    color = input('You arrived at the island unharmed. There is a house with three doors. One red, one blue and one green. Which one will you choose? \n').lower()
elif wait_or_swim == "swim":
    print('You have been eaten by a shark.\nGame Over.')
else:
    print('You chose an option that does not exist. ')

    
if color == "red":
    print('You fell into a room full of fire.\nGame Over.')
    
elif color == "green": 
    print('Congratulations, you have found the treasure!')
    
elif color == "blue":
    print('You fell into a ditch.\nGame Over.')
        
else:
    print('You chose a door that does not exist.\nGame Over.')

In the code above I'm having a problem ending the code. For example, If I wrote down a wrong option in the input. It still continues the game and asks me to answer the next question rather than ending it automatically. Please help. Also, this is my first ever StackOverflow question, so please help me out if there are some formatting problems with the question and if you are having a hard time figuring out the problem I am facing.

Prab-mat
  • 55
  • 1
  • 6
  • you can put this in a function and `return` a value or you can use `exit()`. You can also nest `if` functions – Orbital May 16 '21 at 11:03
  • Thank you but I haven't tried any of those in my course till now. I guess they are on their way to me. Once finished I'll give it a try. – Prab-mat May 16 '21 at 20:01

1 Answers1

0

Insert the if...else statement after the right option. By typing the wrong option, you are printing a statement and then it reads the next code where wait_or_swim is not defined.

if direction == "left":
    wait_or_swim = input('You have come to a lake. There is an island in middle of the lake. Type "wait" to wait for a boat or "swim" to swim accross the lake? \n').lower()
    if wait_or_swim == "wait":
        color = input('You arrived at the island unharmed. There is a house with three doors. One red, one blue and one green. Which one will you choose? \n').lower()
        if color == "red":
            print('You fell into a room full of fire.\nGame Over.')

        elif color == "green": 
            print('Congratulations, you have found the treasure!')

        elif color == "blue":
            print('You fell into a ditch.\nGame Over.')

        else:
            print('You chose a door that does not exist.\nGame Over.')
    
    elif wait_or_swim == "swim":
        print('You have been eaten by a shark.\nGame Over.')
    else:
        print('You chose an option that does not exist. ')
elif direction == "right":
    print('You are crushed by a truck. Game Over!')
else:
    print('You chose an option that doesn\'t exist.')
  • Thank you so much, friend! It worked! Finally! So, I have to format if-else loops in such a way that all right answers go first in if' then the 'else' completing the whole loop. Is there anything else you want to add? – Prab-mat May 16 '21 at 19:59