为什么我的用户输入会重复所有问题?

问题描述 投票:0回答:1
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!!")

第一个问题是正常的,然后其余的问题都在一起。我尝试删除 while true 循环,但它仍然组合在一起。当我添加 else 时,它也会打印出来。

if-statement while-loop user-input
1个回答
0
投票

第一个问题是正常的,然后其余的问题都在一起。

确实如此。 看看你在做什么第一个问题:

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

对于后续问题:

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

你看到区别了吗? 在第一个问题中,您提示用户输入并存储结果。 在剩下的问题中,您只需将它们打印到控制台即可。

如果要提示用户输入并存储结果,请使用

input
并将结果存储在变量中。 就像你在第一个问题中所做的那样。 例如:

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

顺便说一句...您也不需要对字符串值调用

str()
。 将字符串转换为字符串不会做任何有用的事情,但它确实会使代码变得混乱并使您(和其他人)难以阅读。

© www.soinside.com 2019 - 2024. All rights reserved.