如果返回错误答案(python)

时间:2017-02-24 23:39:17

标签: python

我正在研究“学习python the hardway”。如果这是重复的话,我道歉但我真的无法弄清楚我做错了什么。当我输入少于50的东西时,它告诉我再试一次,第二次在另一个函数中调用一个字符串。如果我在第二个条目上输入大于50的相同内容,则在另一个函数中调用另一个字符串。它会从green_dragon()调用并打印出来“两条龙都会让你活着并吃掉你。”感谢您提供的任何见解。我为简单而道歉,哈哈。不得不制作我自己的“游戏,我还没那么有创意,哈哈。”

def gold_room():
    print "You have entered a large room filled with gold."
    print "How many pieces of this gold are you going to try and take?"
    choice = raw_input("> ")
    how_much = int(choice)
    too_much = False
    while True:
        if how_much <= 50 and too_much:
            print "Nice! you're not too greedy!"
            print "Enjoy the gold you lucky S.O.B!"
            exit("Bye!")

        elif how_much > 50 and not too_much:
            print "You greedy MFKR!"
            print "Angered by your greed,"
            print "the dragons roar and scare you into taking less."

        else:
            print "Try again!"
        return how_much

def green_dragon():
    print "You approach the green dragon."
    print "It looks at you warily."
    print "What do you do?"
    wrong_ans = False
    while True:
        choice = raw_input("> ")

        if choice == "yell at dragon" and wrong_ans:
            dead("The Green Dragon incinerates you with it's fire breath!")
        elif choice == "approach slowly" and not wrong_ans:
            print "It shows you its chained collar."
        elif choice == "remove collar"and not wrong_ans:
            print "The dragon thanks you by showing you into a new room."
            gold_room()
        else:
            print "Both dragons cook you alive and eat you."
            exit()

1 个答案:

答案 0 :(得分:1)

too_much = False

if <= 50 and too_much

如果too_much设置为False,为什么期望if表达式计算为true?它永远不会进入if
也可以在循环内移动用户输入。

编辑:

停止你的while循环:

        too_much = True
        while too_much:
            choice = raw_input("> ")
            how_much = int(choice)
            if how_much <= 50:
                print "Nice! you're not too greedy!"
                print "Enjoy the gold you lucky S.O.B!"
                too_much = False
            else:
                print "You greedy MFKR!"
                print "Angered by your greed,"
                print "the dragons roar and scare you into taking less."