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 Instant Objects Method Arguments

Calvin Secrest
Calvin Secrest
24,815 Points

Method arguments

Every time I run this code

import random

class Thief:
    sneaky = True

    def __init__(self, name, sneaky=True, **kwargs):
        self.name = name
        self.sneaky = sneaky

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

    def pickpocket(self):
        return self.sneaky and bool(random.randint(0, 1))

    def hide(self, light_level):
        return self.sneaky and light_level < 10

I get a syntax error I don't understand?

Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
AttributeError: 'Thief' object has no attribute 'favorite_weapon' 

[Mod: changed formatting to ```python -cf]

Are you sure you don't have another file that calls favorite_weapon? Or if you are using the python interactive environment, what did you type when you got that error?

3 Answers

Steven Parker
Steven Parker
231,269 Points

There must be more code involved, nothing shown here would explain a reference to "favorite_weapon".

Can you make a snapshot of your workspace and post the link to it here?

I am getting this same error. Can someone please help me?

Brandon: May I suggest you create a new ticket (because most people won't see comments on old tickets, unless they were looking at it already).

In your new ticket, add the code you are trying to run (be sure to format it with markdown), what you typed to run it, and the exact error you got. That will greatly increase the chance you will get the help you need.

Good luck!