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

igsm '
igsm '
10,440 Points

Attributes interesting test. What happens?

Right.

I set name and weapon attributes when I am creating a new instance. Then, by logic program asks me again to input a value for the name and choose the weapon from the list suggested. See below:

class Character(Combat):
    experience = 0
    hit_points = 10

    def get_weapon(self):
        weapon_choice = raw_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 = raw_input('Name: ')
        self.weapon = self.get_weapon()

        for key, value in kwargs.items():
            setattr(self, key, value)
player = Character(name = 'Chuck', weapon = 'Leg Strike')

Name: Freddie

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

# Now lets check the player
player.name
'Chuck'

player.weapon
'Leg Strike'

What happens with Freddie and Axe?

John Reilly
John Reilly
10,800 Points

Hi Igor

Can you upload your code so we can see what the program is doing behind the scenes? At present I do not know what you are doing with the two user inputs of Freddie and A. I can only assume you are not overwriting the name and weapon. Also why are you setting the player object before taking in the user input?

Thanks

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

They're overwritten by the new values. Those values, as far as your class instance is concerned, don't exist and might as well have not ever existed. Only the current values matter and exist.

igsm '
igsm '
10,440 Points

The question is not why I am doing this, the question is what happens with Freddie and Axe. As I test different ways of using classes and methods, I merely want to know 'what will happen if...'.

P.S. The code is taken from the video lecture and the question is related to this video too.