而True语句在前一个函数中运行。为什么?

时间:2017-06-28 16:44:29

标签: 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 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()

例如,我输入"等待"然后" west"。我期待这个节目在印刷后结束"你决定向西方漂流,那里有护理包掉线。那里必须有武器",而是我被带回start()函数中的raw_input提示符。

它很奇怪,因为start()函数中的初始print语句没有被执行。我必须对如何格式化水平有一个错误的理解。当我离开包含while循环的函数时,我认为while循环将结束。有人可以解释为什么while循环仍在运行以及我将来如何避免这种错误?非常感谢你。

1 个答案:

答案 0 :(得分:0)

一旦python完成执行freefall()函数,它就会回到调用函数的位置(所以elif语句)。它会忽略else语句,并在while True语句的开头重新开始(所以在choice = raw_input("> ")

如果您无法理解代码流,我建议您使用http://www.pythontutor.com/visualize.html逐步查看代码。