Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError?
#1
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!
Reply
#2
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)
    ...
Reply
#3
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'
.
Reply
#4
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)
Reply
#5
Problem Fixed! Thank you!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020