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) Objects Class Methods

Hi. I have typed what you have in "Class Methods" video. I type .battlecry() and get an AttError, you get TypeError why?

So when I type:

class Monster:
   hit_points = 1
   color = 'yellow'
   weapon = 'sword'
   sound = 'roar'

   def battlecry(self):
       return self.sound.upper()

and then I type jubjub = Monster and say jubjub.battlecry() it returns AttributeError. You get a type error, and when I follow the rest of the video, it doesn't work for me. Help please.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I updated your post for formatting.

6 Answers

I was having the same issue:

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

However, I cleared it up by restarting my shell session. I think some of our experimenting in previous videos is causing the error.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Did you type jubjub = Monster or jubjub = Monster()? Those parentheses mean a lot. The first example is making a new variable that points to the Monster class. The second example makes a new variable that points to an instance of the Monster class. Only instances have instance methods (methods that have self as the required first argument)

I typed jubjub = Monster()

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm, strange. When I put your code into my terminal, it works fine.

You're having this problem on your computer or in Workspaces?

I have that problem in workspaces. I'm running a Windows 7 Ultimate OS. No clue if that's the problem or not.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, your OS shouldn't matter if you're using Workspaces.

Can you run the code again and copy and paste the exact error it gives you? It'll probably be 3 or 4 lines long.

Sure just a sec. Strange. Now it's working. Well, thank you for your time anyway. I have no clue what happened yesterday. Thanks again!

Jonell Alvi
Jonell Alvi
4,259 Points

I had the same difficulty as Chase, and it was driving me crazy. Didn't seem like anything was wrong.

I think it might be a workspaces thing, because when I exited python, returned to the command line, then restarted the python shell, it worked as expected.

Thought I'd update the thread in case anyone else encounters this problem.

:-)

Douglas Sangster
Douglas Sangster
3,011 Points

This problem occurs if you don't import the most recent version of Monster. If you're still working in the same console state as before you updated monster.py with the battlecry method then you won't have imported the new version of Monster with battlecry.

Just exit() , python, and import Monster again. I've tested and you can't just import Monster again in the same console window without using exit(), it doesn't seem to take in the new information for whatever reason.

Also could you please explain the challenge after the video? I get everything correct except when I type .upper() or .lower() it says to me "Expected 'We are open from 10-2' received "WE ARE OPEN FROM 10-2" or "we are open from 10-2" so I don't know what I should replace .upper/lower with because when I leave that empty the command won't read in the challenge.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That challenge doesn't require .upper() or .lower().

So then could you type in the answer for the challenge please so I can compare what I got to the answer?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I don't like to give away answers, but you just have to return the string "We're open from {} to {}." with the first placeholder filled in withself.openand the second filled withself.close`. There should only be one uppercase letter (the first one), it needs to end with a period, and has to have the two times in it.

I don't like to cheat but I'm stuck on it because I have no idea what to put where .upper would go because if I don't put .upper or .lower the code won't process. I'll try again thanks.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Chase Wolff It's really hard to help with code I can't see :)

Yeah. Thanks anyway.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Chase Wolff sorry if that sounded like I don't want to help you. I definitely do want to help. I just need to be able to see your code to help you find what's wrong with it. Can you post it so we can all help?

This is getting very frustrating because I refuse to move on unless I finish the challenge so that I can understand. I don't understand why my code won't work. I have used everything you have taught me in the videos, and I can't get an answer no matter how many different times I put in various versions of the code!

class Store: open = 9 close = 2 format = "We're open from self.open to self.close."

def hours(self): return self.format()

Kenneth Love
Kenneth Love
Treehouse Guest Teacher
class Store:
    open = 9
    close = 17

    def hours(self):
        return "We're open from {} to {}.".format(self.open, self.close)

That passes perfectly for me. What is your code doing differently?

I was confused because I applied what I learned in the video, but I didn't understand what .format meant, or how I needed to change the code from what the video said.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

We didn't cover .format() in the video because we covered it in Python Basics. We also used it quite a bit in Python Collections. Sorry if it came up without any warning.

Also, as far as videos and code challenges go, I tend to not make you repeat exactly what was in the video in a code challenge. I want you to apply the ideas yourself, so the implementations are often a little different.

I'm always happy to read ideas for improving a course, quiz, or code challenge. kenneth@teamtreehouse.com

Chen Wang
Chen Wang
7,371 Points

I think maybe you can try python3, rather than earlier version.