2017-07-23 33 views
0

我是一名初學者程序員。我想創建一個用戶輸入影響遊戲過程的遊戲。我有一種卡住的一開始。python 3.x中基於文本的探險幫助

def displayIntro(): 
    print("You wake up in your bed and realize today is the day your are going to your friends house.") 
    print("You realize you can go back to sleep still and make it ontime.") 

def wakeUp(): 
    sleepLimit = 0 
    choice = input("Do you 1: Go back to sleep or 2: Get up ") 
    for i in range(3): 
     if choice == '1': 
      sleepLimit += 1 
      print("sleep") 
      print(sleepLimit) 
       if sleepLimit == 3: 
        print("Now you are gonna be late, get up!") 
        print("After your shower you take the direct route to your friends house.") 

     elif choice == '2': 
      print("Woke") 
      whichWay() 

     else: 
      print("Invalid") 

def whichWay(): 
    print("After your shower, you decide to plan your route.") 
    print("Do you take 1: The scenic route or 2: The quick route") 
    choice = input() 
    if choice == 1: 
     print("scenic route") 
    if choice == 2: 
     print("quick route") 



displayIntro() 
wakeUp() 

我有一些錯誤,我試圖自己解決它們,但我很掙扎。

1)我只希望玩家能夠回到3次睡眠,第三次我想要一個消息出現,另一個功能運行(還沒有做出)。 2)如果玩家決定醒來,我希望whichWay()運行,但它的確如此,而不是退出循環而是直接回到那個循環,並詢問玩家是否想再次醒來,我沒有想法如何解決這個問題。

3)有沒有更好的方法可以製作這樣的遊戲?

謝謝你的時間,並希望你的答案。

回答

0

下面的代碼應該可以工作。
1.我將「choice = input」(「你是1:回去睡覺還是2:起牀」)行移動到for循環中。
2.我在elif塊的末尾添加了一個break語句。

def wakeUp(): 
sleepLimit = 0 

for i in range(3): 
    choice = input("Do you 1: Go back to sleep or 2: Get up ") 
    if choice == '1': 
     sleepLimit += 1 
     print("sleep") 
     print(sleepLimit) 
     if sleepLimit == 3: 
      print("Now you are gonna be late, get up!") 
      print("After your shower you take the direct route to your friends house.") 

    elif choice == '2': 
     print("Woke") 
     whichWay() 
     break 

    else: 
     print("Invalid") 
+0

謝謝你這是工作......所以爲了將來的參考,我需要添加一個新的函數調用後,如果它的內部循環中的break語句? – Question

+0

如果您希望立即退出循環並達到所需條件,則需要添加break語句。在你的代碼中,所需的條件是一個「走哪條路」的決定。一旦函數whichWay()返回它的輸出,就不需要繼續for循環,因此它必須退出。 –

+0

好的。再次感謝你 – Question