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 Your first method

Oliver White
Oliver White
5,657 Points

Picking up python for first time in a while and struggling with methods

I am getting a super basic error and I just cant work out why

running this it keeps giving me a name error

class Student: name = "Oliver" def praise(self):
return = print("Your're doing a great job, {}".format(name))

I also tried running this

class Student: name = "Oliver" def praise(self):
return = print("Your're doing a great job, {}".format(self))

Its going to be something really simple

3 Answers

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

The code you provided in your last comment worked just fine for me. I had to indent things, of course though. Make sure your code is indented correctly like the snippet I shared in my previous comment. Since Python is particular about it's indentation, when posting code here in the forum, it's helpful to wrap all of the code in triple backticks (same button with the tilde ~). Putting three backticks before and after your code will allow it to maintain its formatting so I can it as you have it in the challenge.

Regarding instantiating the class and calling its method, the back end of the challenge is doing that automatically here. It's set up to do what's needed and check the results against the code that you provide so you don't need to worry about that in the challenges unless it specifically asks you to do so.

Here is the code in its entirety that you can copy/paste into the challenge and try to submit.

class Student: 
    name = "Oliver"
    def praise(self): 
        return "You're doing a great job, {}!".format(self.name)

This is all that is needed to pass here. 👍

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

You're extremely close here Oliver White !

Just a few small adjustments needed.

  1. We don't use an = when returning a value. We just write the return keyword and then place what we want to return.

  2. We don't want to return a print() here, just the formatted string itself

  3. We need to combine both of your attempts for what to format the string with and specify self.name

class Student:
    name = "Oliver"
    def praise(self):
        return "You're doing a great job, {}".format(self.name)
Oliver White
Oliver White
5,657 Points

Hello

Thanks for the help - sadly this code does not run - it just says bummer:try again - ( we are passed the name issue) The "=" not return was my fault - not done python in a while and the print was something I added as I could not work out what it seemed not to be returning

This is the code I am running class Student: name = "Oliver" def praise(self): return "You're doing a great job, {}!".format(self.name)

It is object oriented python challenge one --

full question

Challenge Task 1 of 1 This class should look familiar!

First, I need you to add a method named praise to the Student class. It should take the self argument. Then, inside the praise method, return a positive message about the student using the name attribute.

As an example, it could say "You're doing a great job, Jacinta!" or "I really like your hair today, Michael!".

Feel free to change the name attribute to your own name, too!

The part I am also unclear on is how to we call/execute the class and method -- in the lesson we were using the repl and assigning values to create objects to use as the instance to call the class and function ( sorry if my terminology needs work, i am just starting python) -- is that the part that is missing ? 


Thanks in advance 

Oliver
Oliver White
Oliver White
5,657 Points

AHHHHHHHHHHHH

I am still learning this indent thing -

I thought as I wanted the return to happen at the same time as the method it should be on the same indentation !!

Sorry -- these are the mistakes that break you on distance learning courses -- in person the trainer would have laughed - shown me how to use the tab key and hopefully offered me a biscuit/cookie !!

Thanks

Oliver

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

Haha, no worries at all my friend! We're all learning! I'm glad it was just a simple issue.

But still... here you go 🍪😃