![]() |
AttributeError? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: AttributeError? (/thread-16057.html) |
AttributeError? - ghost0fkarma - Feb-13-2019 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! RE: AttributeError? - Larz60+ - Feb-13-2019 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) ... RE: AttributeError? - ghost0fkarma - Feb-13-2019 Sorry to be a bother but I still don't understand. I keep receiving the error .
RE: AttributeError? - stullis - Feb-13-2019 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) RE: AttributeError? - ghost0fkarma - Feb-13-2019 Problem Fixed! Thank you! |