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

Jack Pincus
Jack Pincus
1,673 Points

NameError: kwargs is not defined

Doing the inheritance in OOP python. it keeps spitting back an error, I can't seem to find out why

class Character:
    experience = 0
    hit_points = 10

    def get_weapon(self):
        weapon_choice = input('choose [S]word, [A]xe, or [B]ow: ')
        while weapon_choice not in 'SAB':
            weapon_choice = input('{} is not a choice. You must choose [S]word, [A]xe, or [B]ow: '.format(weapon_choice))
        if weapon_choice in 'SAB':
            if weapon_choice == 'S':
                weapon = 'sword'
            elif weapon_choice == 'A':
                weapon = 'axe'
            else:
                weapon = 'bow'
            return weapon
    def __init__(self, **kwargs):
        self.name = input('Name: ')
        self.weapon = self.get_weapon()

        for key, value in kwargs.items():
            setattr(self, key, value)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

How are you executing the code? Please post the code that causes error.

1 Answer

Steven Parker
Steven Parker
229,644 Points

I couldn't replicate your error, could there be more code that you didn't include?

But I did discover a different issue - did you mean to use self.get_weapon(), or should that be get_weapon(self)?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

self.get_weapon() is the correct reference to a method on this instance.