2015-05-09 23 views
0

我很新的編程。我試圖做一個簡單的冒險遊戲,但我已經堅持試圖通過第一個功能。這是我到目前爲止的代碼:給出提示無法獲得更多的功能在初學Python代碼運行

def find_shelter(): 
    print "Your crew needs to find shelter for the first night.\n" 
    print "Do you choose....\n" 
    print "The cave, it is near the water. You will be safe from wind, but there may be animals inside the cave.\n" 
    print "The beach, it is very comfortable. You can see boats, but we will be exposed to weather.\n" 
    print "The jungle, it has many trees. You will be safe from storms. However, there are many animals.\n" 
    print "The mountain, it is very high. You will be safe from the jungle animals, but you may fall off the mountain.\n" 
    choice = raw_input("Select A for cave, B for beach, C for jungle, or D for mountain. ") 
    return choice 

def oh_no_cave(): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "There was a flood in the cave, one person drowned and is dead." 
    crew = crew - 1 
    print "There are now %s people left in your crew.", crew 
    return crew 

def oh_no_beach(): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "The storm rained on you. One person was struck by lightening and is dead." 
    crew = crew - 1 
    print "There are now %s people left in your crew.", crew 
    return crew 

def oh_no_jungle(): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "But, you were safe in the jungle from the storm. Everyone is ok." 
    print "There are still %s people left in your crew.", crew 

def oh_no_mountain(): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "You all get very wet on the mountain, but everyone is ok." 
    print "There are still %s people left in your crew.", crew 

choice = [] 
print "You are going on a cruise with your family and friends.\n" 
crew = raw_input("Enter the number of people on the cruise, must be at least four: \n") 
print "Oh no! Your crew has just shipwrecked on a desolate island." 
print "All food and supplies were lost at sea, and the captain has died too. :(" 
print "On the island there is a beach, a river, a jungle, a mountain, and a cave." 

if crew != 0: 
    find_shelter() 
    if choice == "a": 
     oh_no_cave() 
    elif choice == "b": 
     oh_no_beach() 
    elif choice == "c": 
     oh_no_jungle() 
    elif choice == "d": 
     oh_no_mountain() 
else: 
    print "OH NO! Everyone in your crew has died! It's all over...." 
    print "Completely over." 

後供用戶選擇A,B,C,d,什麼都不會發生用戶作出選擇之後。請讓我知道我能做些什麼來解決這個問題,也歡迎任何其他提示。我後來打算檢查以確保用戶輸入了至少4名機組人員,但不知道如何去做。

+2

一般來說,你應該在你的問題後不爲紐帶,也可以爲您降低代碼來重現您的問題 – EdChum

+1

錯誤是通過使用鍵盤造成所需的最小;它*不支持用戶輸入*;我不認爲你在問這個問題,所以我省略了。 –

+0

我可以保證'crew'永遠不會等於0,你總是調用'find_shelter()'不管輸入什麼 –

回答

1

提供的代碼存在一些問題,我試圖修復其中的一些問題。

問題在if crew != 0:開始,這是一個if語句,這取決於條件,因爲這隻會跑出一次,因爲你需要給用戶多次提示,直到所有的船員都死了,你必須使用while crew != 0:和不要忘記在while循環中更改變量crew的值,否則它將是一個無限循環。

的另一大問題是線crew = raw_input("Enter the number of people on the cruise, must be at least four: \n")這將返回一個字符串,因此你將無法算術運算應用到這一點,所以你需要使用crew = int(raw_input("Enter the number of people on the cruise, must be at least four: \n"))

現在內將其轉換成一個intwhile循環您正在調用確實正在更改變量crew(它不是全局定義的變量)的值的函數,但不會將返回的值存儲到任何變量中。

你也失蹤了else條款來處理用戶在其中輸入除"a", "b", "c", "d"

任何其他輸入通過固定上述所有問題的問題最終的代碼看起來是這樣的:

def find_shelter(): 
    print "Your crew needs to find shelter for the first night.\n" 
    print "Do you choose....\n" 
    print "The cave, it is near the water. You will be safe from wind, but there may be animals inside the cave.\n" 
    print "The beach, it is very comfortable. You can see boats, but we will be exposed to weather.\n" 
    print "The jungle, it has many trees. You will be safe from storms. However, there are many animals.\n" 
    print "The mountain, it is very high. You will be safe from the jungle animals, but you may fall off the mountain.\n" 
    choice = raw_input("Select A for cave, B for beach, C for jungle, or D for mountain. ") 
    return choice 

def oh_no_cave(crew): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "There was a flood in the cave, one person drowned and is dead." 
    crew = crew - 1 
    print "There are now %s people left in your crew."% crew 
    return crew 

def oh_no_beach(crew): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "The storm rained on you. One person was struck by lightening and is dead." 
    crew = crew - 1 
    print "There are now %s people left in your crew."% crew 
    return crew 

def oh_no_jungle(crew): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "But, you were safe in the jungle from the storm. Everyone is ok." 
    print "There are still %s people left in your crew."% crew 

def oh_no_mountain(crew): 
    print "OH NO! There was a very big storm in the night! It rained and the wind was very strong." 
    print "You all get very wet on the mountain, but everyone is ok." 
    print "There are still %s people left in your crew."% crew 

#choice = [] 
print "You are going on a cruise with your family and friends.\n" 
crew = int(raw_input("Enter the number of people on the cruise, must be at least four: \n")) 
print "Oh no! Your crew has just shipwrecked on a desolate island." 
print "All food and supplies were lost at sea, and the captain has died too. :(" 
print "On the island there is a beach, a river, a jungle, a mountain, and a cave." 

while crew != 0: 
    choice = find_shelter() 
    if choice == "a": 
     crew = oh_no_cave(crew) 
    elif choice == "b": 
     crew = oh_no_beach(crew) 
    elif choice == "c": 
     crew = oh_no_jungle(crew) 
    elif choice == "d": 
     crew = oh_no_mountain(crew) 
    else: 
     print "Please enter a valid choice." 
else: 
    print "OH NO! Everyone in your crew has died! It's all over...." 
    print "Completely over." 
+0

感謝anmol_uppal幫助我清理了一下,並得到我的其他if語句。然而,我嘗試使用while,但不得不回去使用if語句,因爲它保持循環遍歷find_shelter,直到隊員爲0,然後才運行oh_no函數 –

+0

沒關係,我想我需要把choice = find_shelter while while循環之外 –

+0

是的現在它在一定程度上取決於你@PistolPete,你如何管理程序的流程。很高興幫助你。 – ZdaR

0

你AREN將函數的返回值賦給變量choice。你應該做這樣的事情:

choice = find_shelter()