Python 而上一个函数中的True语句正在运行。为什么?

Python 而上一个函数中的True语句正在运行。为什么?,python,loops,while-loop,Python,Loops,While Loop,我正在尝试制作一个非常简单的基于文本选择的游戏来学习python。在运行到最后之后,我遇到了前面创建的while循环,我不确定它是如何工作的。有人能解释一下计算机所走的路吗 我的代码在这里: def dead(message): print message, "You have lost, but you can try again if you'd like." exit(0) def start(): print """You're sitting in a dar

我正在尝试制作一个非常简单的基于文本选择的游戏来学习python。在运行到最后之后,我遇到了前面创建的while循环,我不确定它是如何工作的。有人能解释一下计算机所走的路吗

我的代码在这里:

def dead(message):
    print message, "You have lost, but you can try again if you'd like."
    exit(0)

def start():
    print """You're sitting in a dark room. You hear the dull hum of the engine and coughing
from what seems to be like 50 or more people. The last thing you remember was walking out
of a convenience store before you felt a sharp pain at the back of your head and blacked out.
You're years of tactical army training tell you that you must've gotten kidnapped. There's a sack
over your head to prevent you from seeing anything. You feel a collar on your neck...There's a
faint beeping. What do you do? Take off the collar? Or wait to see what is happening?"""
    while True:
        choice = raw_input("> ")

        if "take off" in choice:
            dead("Your hands are free and you pull off the collar. The beeping stops, but then the collar explodes...killing you instantly. You had no idea it was a bomb attached to your neck.")

        elif "wait" in choice:
            print """You decide to wait it out. That collar might even be a bomb on your neck.
All of a sudden the bag is taken off your head by an unknown figure. You see 50+ people
looking around just as confused as you. Just as you are about to say something, the floor opens up
and all of you fall through. You're now in freefall. \n"""
            freefall()

        else:
            print "You need to decide."


def freefall():
    print """As you fall, you hear the other people screaming on the way down. However, with your calm
composure and army training you realize everyone is equipped with a parachute. The ground is still far
away and you try to look around to orient yourself. You and all the others seem to be dropping into a island
with a giant active volcano in the middle. Out of nowhere, the collar begins to speak to you.

\"Welcome to the Trial. I, the host, have left weapons scattered throughout the island. There is only one out of all
of you that I will return to their normal lives. The rest of you will die here. If you attempt to leave the island, the
collar will detonate. As I said before, only one of you will be taken back...you know what that means\"

You aren't surprised. You've seen the Hunger Games. With the wind blowing past you, you look to the East and see buildings.
To the West, you see care packages being dropped. No doubt there are weapons at both locations that you can loot. Which direction
do you want to aim for?
"""
    choice = raw_input("> ")
    if "east" in choice or "East" in choice:
        print "You decide to drift towards the East where you saw the buildings. There must be weapons there."

    elif "west" in choice or "West" in choice:
        print "You decide to drift towards the West where there are care packages dropping. There must be weapons there."

    else:
        dead("You decided not to drop in the East or West. A giant hand slaps you out of the sky and you hit the ground and die instantly")




start()
例如,我先键入“wait”,然后键入“west”。我原以为程序会在打印“你决定向西漂移,那里有护理包掉落。那里一定有武器”后结束,但我被带回到start()函数中的原始输入提示符


这很奇怪,因为start()函数中的初始print语句没有执行。我必须有一个有缺陷的理解如何格式化的水平。我认为一旦我离开包含while-True循环的函数,while-True循环就会结束。有人能解释一下为什么while循环仍然在运行,以及我将来如何避免这个错误吗?非常感谢您。

一旦python执行完
freefall()
函数,它将返回调用函数的位置(即
elif
语句)。它将忽略
else
语句,并在
的开头重新开始,而True
语句(因此在
choice=raw\u input(“>”)


如果您在理解代码流方面遇到困难,我建议您使用查看代码的逐步执行。

要退出循环,请使用
中断
,要退出函数,请使用
返回
。在这种情况下,当您希望退出函数时,只需在您希望退出循环的条件下执行一个
中断