user3362503
user3362503

Reputation: 1

Variables reset when calling method

I'm creating a text-based game that requires you to search two places to move forward in the game, but it also has two dummy options to lead you astray. These are not important. The problem is that once the method is called again after heading down one of the 'if' options, and one of the variables is set to true, it sets it back to false at the very top.

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()

Upvotes: 0

Views: 423

Answers (1)

Pedro
Pedro

Reputation: 21

"While" loop will execute again and again until the condition is false, so you don't need to call the function again from within the loop.

Delete or comment out all calls to marketplace(), and it will work fine.

Also you don't need to use continue either if you are using an if - elif, because it will execute only one of the sentences.

Upvotes: 1

Related Questions