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 trialrhupp
11,019 PointsAttributeError: 'Thief' object has no attribute 'pickpocket'
I'll move past this, but curious whether anyone could tell me why I'm running into errors running this code:
import random
class Thief:
sneaky = True
def pickpocket(self):
return bool(random.randint(0, 1))
I ran the same commands as Kenneth within the console --
>>> from characters import Thief
>>> kenneth = Thief()
>>> kenneth.pickpocket()
-- but when I hit ENTER, I got an AttributeError instead of a boolean result (Kenneth got 'False' in this instance).
Can anyone explain where I went wrong?
3 Answers
rhupp
11,019 PointsI figured it out. Looks like Workspaces doesn't like that extra line within the class. When I changed the code to --
import random
class Thief:
sneaky = True # There used to be a space between this line and the next one
def pickpocket(self):
return bool(random.randint(0, 1))
-- I got the proper output in the console:
>>> from characters import Thief
>>> kenneth = Thief()
>>> kenneth.pickpocket()
False
>>>
So all's well. :) Hope this helps anyone running into the same error!
Deividas Paulauskas
3,661 PointsI had the same problem. Exiting from python and entering again in console solves the problem. For some reason after changing the file content import doesn't update the code from the file.
KRIS NIKOLAISEN
54,972 PointsNot sure. I copy/pasted your code including the commands and received False just like the video.
rhupp
11,019 PointsHmm. Maybe it's just my version of the console that doesn't like the extra space within this class. Either way, thank you for the quick response!