我正在創建一個基於文本的遊戲,需要您搜索兩個地方纔能在遊戲中前進,但它也有兩個虛擬選項將您引入歧途。這些並不重要。問題在於,一旦方法在標題爲「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()
不記得'marketplace()'。相反,使用「繼續」。你沒有利用你的循環。 – zondo