Python Forum

Full Version: AttributeError?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
class player:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def player_is_attacked(self, dmg_taken):
        return self.health - dmg_taken

    def player_is_healed(self, healing_recieved):
        pass

def beginning():
    player_name = input("What is your name? ")
    main_player = (player_name, 100)
    user_input = input("""
    
    1. Player is attacked
    2. Player is healed
    3. Test your luck
    
                        """)

    if user_input == "1":
        print(main_player.player_is_attacked(20))

beginning()
So I'm trying to learn how to use classes. I want to print out the value of 80 when the player takes 20 damage. Any help or tips is highly appreciated!
once you have instantiated player in the beginning function, you can access any of the class methods or variables defined with self.
example:
def beginning():
    current_player = player()
    ...
    print(current_player.name)
    current_player.player_is_attacked(dmage)
    ...
Sorry to be a bother but I still don't understand. I keep receiving the error
Error:
AttributeError: 'tuple' object has no attribute 'player_is_attacked'
.
Look at line 14:

main_player = (player_name, 100)
The variable main_player is a tuple. To make a player() object, it needs to be:

main_player = player(player_name, 100)
Problem Fixed! Thank you!