如果返回错误的答案(python)
我正在通过“学习python hardway”。我很抱歉,如果这是重复的,但我真的不知道我做错了什么。当我输入小于50的数字时,它告诉我再次尝试,并在第二次调用另一个函数中的字符串。如果我在第二个条目中输入大于50的同一个东西,它会在另一个函数中调用另一个字符串。它会从green_dragon()调用并打印出“两条龙都活着烹饪你并且吃掉你。”感谢您提供的任何见解。我为简单而道歉,哈哈。不得不让我自己“的游戏,我不认为创意呢,哈哈。如果返回错误的答案(python)
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()
too_much = False
if <= 50 and too_much
如果too_much
设置为False,为什么你所期望的,如果要计算的表达式为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."
我将它改为True它仍然做同样的事情,但现在无法打印出来的字符串。 –
我试图在这些课程中将自己的文字游戏作为练习的一部分 –
没有它,它会在一次运行后停止。我试图让它一直要求输入,直到它小于或等于50 –
你永远不会改变'too_much',所以第一个'if'永远不会成功。 – Barmar
在第二个函数中,你永远不会改变'wrong_ans',所以第一个'if'永远不会成功。 – Barmar
这两个变量有什么意义? – Barmar