Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Instance Methods

jasper printemps
jasper printemps
4,398 Points

Help with class Character please

class Player:
    experience = 0
    hitpoints = 10

    def get_weapon(self):
        weapon_choice = input("Weapon ([S]word, [A]xe, [B]ow :").lower()

        if weapon_choice in "sab":
            if weapon_choice == 's':
                return 'sword'
            elif weapon_choice == 'a':
                return 'axe'
            else:
                return 'bow'
        else:
            return self.get_weapon()

    def __init__(self, **kwargs):
        self.name = input('Name: ')
        self.weapon = self.get_weapon()

        for key,value in kwargs.items():
            setattr(self,key,value)


Player()
Name: >? jasper

Weapon ([S]word, [A]xe, [B]ow :>? 'a'

Weapon ([S]word, [A]xe, [B]ow :>? A

<player_class.Player object at 0x03319CF0>

Fixed formatting of code.

Where is it you are stuck? What is the error you are getting?

2 Answers

player.name()

Traceback (most recent call last):

File "<input>", line 1, in <module>

NameError: name 'player' is not defined

You are getting 'player' is not defined because player variable needs to exist as a Player() object before you can try to call .name on it. Also your next error will have to do with .name() name is an attribute inside the Player class not a method. Methods normally have parenthesis.

So if you did something like this before calling player.name

player = Player()
player.name

it should contain the name you input into it.

jasper printemps
jasper printemps
4,398 Points

from player_class import Player

Player()

Name: >? jasper

Weapon ([S]word, [A]xe, [B]ow :>? s

<player_class.Player object at 0x03DC9B70>

player.name()

Traceback (most recent call last):

File "<input>", line 1, in <module>

NameError: name 'player' is not defined