jeudi 30 novembre 2017

Calling a class function in an IF statment, keeps returning True

I have a class Player() with a function called use_potion(). When I am using use_potion() in an IF statement, at first it works fine. When the return value for use_potion changes, the if statement ignores the change!

Here's a section of the code:

class Player():
    def __init__(self):
        inventory = ["potion"]

    def has_potion(self):
        return any(item == "Potion" for item in self.inventory):

In another module:

from Player import Player

def available_actions():
    moves = ["go east","go west"]
    if Player().has_potion():
        moves.append("use potion")
    return moves

When I call available_actions(), it returns all three moves as it is supposed to. But when "potion" is removed from the Player().inventory, available_actions STILL returns all three moves instead of only "go east" and "go west". I have no idea why this is happening.

Aucun commentaire:

Enregistrer un commentaire