2016-03-01 75 views
0

我正在創建一個基於文本的遊戲,需要您搜索兩個地方纔能在遊戲中前進,但它也有兩個虛擬選項將您引入歧途。這些並不重要。問題在於,一旦方法在標題爲「if」選項之一後再次被調用,並且其中一個變量被設置爲true,它會在最上方將其設置爲false。調用方法時重置變量

def marketplace(): 
    searchArmorer=False 
    searchBlacksmith=False 
    while (searchArmorer==False and searchBlacksmith==False): 
     print("Search the:") 
     search=input("\n A. The blacksmith \n B. Crocodile Shop \n C. Armorer \n D. Exotic Cheese shop \n") 
      if search=="A": 
      searchBlacksmith=True 
      print("You find some dusty looking copper swords in the corner that the blacksmith says you can borrow. If you haven't got it already, now all you need is some armor. \n") 
      marketplace() 
     elif search=="B": 
      croc=input("You open the door to have a crocodile leap out at you! Will you attempt to fight it, or run away? \n A. Fight \n B. Flee \n") 
      #croc fight 
      if croc=="A": 
       print("You have died. Crocodiles are dangerous.") 
       break 
      elif croc=="B": 
       print("You escape back to the village square. \n") 
       marketplace() 
      #end of croc fight 
     elif search=="C": 
      searchArmorer=True 
      print("You ask if the armorer has any cheap armor, but he says the cheapest he has is leather. It's not your first choice, but beggars can't be choosers. \n") 
      marketplace() 
     elif search=="D": 
      print("You see nothing that could help you on your quest. \n") 
     marketplace() 
+3

不記得'marketplace()'。相反,使用「繼續」。你沒有利用你的循環。 – zondo

回答

1

「While」循環將一次又一次地執行,直到條件爲假,因此您不需要再次從循環內調用該函數。

刪除或註釋掉所有對marketplace()的調用,它會正常工作。

如果您使用的是if-elif,您也不需要使用continue,因爲它只會執行其中一個句子。

+0

我必須調用它兩次,一次沿着armorer路徑,一次沿着鐵匠路徑。然而,一旦它重新啓動,它就會重置,因爲我必須啓動變量。我正在尋找解決方法。 – user3362503