-5
while True
    game2=input("Welcome to Treasure island!  Your mission is to find the treasure. You find two different paths. Left or Right?")
    if str(game2)==str("right"):
        print("GAME OVER! You fell down a hole")
    elif str(game2)==str("left"):
        print("CONGATULATIONS! You made it")
    print("You are being chased by an angry mob of monkeys. What do you do? Give them a banana or hide?")
    if str(game2)==str("give them a banana"):
        print("GAME OVER! You didnt have enough bananas")
    elif str(game2)==str("hide"):
        print("CONGRATULATIONS! You made it")
    print("You found an old abandond cabin. Do you go in?")
    if str(game2)==str("no"):
        print("GAME OVER! You were eaten by a hungry bear")
    elif str(game2)==str("yes"):
        print ("CONGRATULATIONS! You got away from the forest")
    print("You find three doors 1,2,and 3. Which do you pick?")
    if str(game2)==str("1"):
        print("GAME OVER! You fell into a dark pit")
    elif str(game2)==str("3"):
        print("GAME OVER! The room was filled with toxic gas")
    elif str(game2)==str("2"):
        print("You chose the right door")
    print("You found a dark room with three treasure chests inside. You can only open one. Which do you pick 1,2,or 3?")
    if str(game2)==str("3"):
        print("GAME OVER! The treasure chest was filled with spiders")
    elif str(game2)==str("2"):
        print("GAME OVER! You opened an empty treasure chest")
    elif str(game2)==str("1"):
        print ("CONGRATS, YOU FOUND THE TREASURE!!")

The first question comes by itself as normal, then the rest of the questions come all together. I tryed removing the while true loop but it still comes all together. When i add else, it prints that too.

2 Answers 2

1

The first question comes by itself as normal, then the rest of the questions come all together.

Indeed. Look at what you're doing for the first question:

game2 = input("Welcome to Treasure island!  Your mission is to find the treasure. You find two different paths. Left or Right?")

And for subsequent questions:

print("You are being chased by an angry mob of monkeys. What do you do? Give them a banana or hide?")

Do you see the difference? In the first question you're prompting the user for input and storing the result. In the remaining questions you're just printing them to the console.

If you want to prompt the user for input and store the result, use input and store the result in a variable. Just like you do in the first question. For example:

game2 = input("You are being chased by an angry mob of monkeys. What do you do? Give them a banana or hide?")

As an aside... You also don't need to call str() on string values. Converting a string to a string doesn't do anything useful, but it does clutter the code and make it difficult for you (and others) to read.

Sign up to request clarification or add additional context in comments.

Comments

-1

There are a few bugs and improvements you can fix.

Bug: Not "collecting" return value of input

Except for your first input() which did game2 = input("Welcome ..."), the rest are missing the game2 = assignment in order to capture what the user had input.

Furthermore, with the exception of the first "fork" (in your game route), the rest are doing print instead of input. You would need input to capture what your user types.

Bug: Not restarting game upon "game over"

For example, in line 4, you are printing "GAME OVER". Logically, you would want to restart the game and go back to line 2. However, your code will skip the elif in line 5 and 6, and continue to line 7.

Improvement: Redundant casting to str

The result of input(...) has type str, hence casting str(game2) is casting str to str which is redundant.

Likewise, "left" is also type str and does not need casting to str.

You can verify the type by print(type(game2)) or print(type("left"))

Improvement: Exit game after winning

If you want to exit the progam when the user (finally) wins the game and get the treasure, you should allow some way to exit the while True loop.

After bugfixs and improvements

Fixing the bugs and adding improvement, your code should look like:

while True:
    game2 = input("Welcome to Treasure island!  Your mission is to find the treasure. You find two different paths. Left or Right?")
    if game2 == "right": # remove unnecessary casting
        print("GAME OVER! You fell down a hole")
        continue # restart game: start from the top of the whole loop again
    elif game2 == "left": # improve readability by having spaces around ==
        print("CONGATULATIONS! You made it")

    # Leave blank line here to improve readability
    game2 = input("You are being chased by an angry mob of monkeys. What do you do? Give them a banana or hide?") # assign output of input to game2
    if game2 == "give them a banana" :
        print("GAME OVER! You didnt have enough bananas")
        continue
    elif str(game2)==str("hide"):
        print("CONGRATULATIONS! You made it")

    # ...

    # other parts of if and elif
    elif game2 == "1":
        print ("CONGRATS, YOU FOUND THE TREASURE!!")
        break # end game: exit (break out) from the loop

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.