2017-06-28 73 views
-2

我想做一個非常簡單的基於文本選擇的遊戲來學習python。在運行到最後,我碰到了我之前創建的while循環,我不確定它是如何工作的。有人可以解釋計算機正在使用的路徑嗎?在前面的函數運行中的True語句。爲什麼?

我的代碼是在這裏:

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() 

例如,I型 「等待」,然後選擇 「西進」。我打算在打印完「你決定漂向西方護理包丟失的地方」這個程序後,程序纔會結束,必須有武器存在,但是我會回到start()函數的raw_input提示符。

這很奇怪,因爲start()函數中的初始打印語句沒有執行。我必須對如何格式化水平有一個有缺陷的理解。我認爲,只要我離開包含True循環的函數,True循環就會結束。有人可以解釋爲什麼while循環仍在運行,以及我如何避免將來出現這種錯誤?提前謝謝你。

+1

要退出一個循環使用'break',退出一個函數使用'return'。在這種情況下,如果您希望退出該功能,只需按照您希望退出循環的條件執行「中斷」。 – cdarke

回答

0

一旦python完成執行freefall()函數,它將返回函數的調用位置(所以elif語句)。它會忽略else聲明,並在聲明while True的從頭再來(因此在choice = raw_input("> ")

如果你無法理解的碼流,我可以推薦使用http://www.pythontutor.com/visualize.html看到代碼執行一步一步。

相關問題