调用方法时重置变量

时间:2016-03-01 19:03:19

标签: python loops python-3.x methods text-based

我正在创建一个基于文本的游戏,要求你搜索两个地方以在游戏中向前移动,但它也有两个虚拟选项让你误入歧途。这些并不重要。问题是,在向下选择一个'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()

1 个答案:

答案 0 :(得分:1)

"虽然"循环将一次又一次地执行,直到条件为假,因此您不需要在循环内再次调用该函数。

删除或注释掉对marketplace()的所有来电,它会正常工作。

如果您使用if - elif,也不需要使用continue,因为它只会执行其中一个句子。

相关问题