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 Challenge (method Interactivity) NEED HELP PLEASE

Hello, I hope you're doing good!

I need a few understanding of concepts.

1) Why is my code showing me an error in my workspace ( 1 positional argument required "self"), but when I submitted my code in the challenge it worked and I completed the challenge.

This is my code;

class Student:
    name = "Your Name"

    def praise(self):
        return "You inspire me, {}".format(self.name)

    def reassurance(self):
        return "Chin up, {}. You'll get it next time!".format(self.name)

    def feedback(self,grade):
        if grade > 50:
            return(self.praise())

        elif grade < 50:
            return(self.reassurance())

These are the inputs I gave in my code;

from file2 import Student

me = Student

me.feedback(me,15)

There is one more thing that I noticed is that when I type "self" in the workspace area like this (below);

def feedback(self,grade):
    if grade > 50:
          return(self.praise(self))

The code works, but when I did it like this in the challenge it gave me an error, basically in the challenge no "self" implementation is needed while in my workspace it gives me the error mentioned above. Please explain to me with proper details as I am a kind of a perfectionist and I get really bothered when I can't understand something properly.

2) I would want to know are these the proper inputs I gave? also I quite can't properly understand what does "me" become and what is its purpose and how to use it. Also how to properly use this class?

3) I got an idea of the argument "self" which is required when defining a method but I still can't quite understand it properly, like is it a constant value? or does it automatically become whatever argument is given in, instead of "self"? what is "self" basically?

1 Answer

1) Although your class code passes the challenge it shouldn't have. What if grade is 50?

2) To create an instance of the class and test:

>>> from file2 import Student                                                      
>>> me = Student()                                                                 
>>> me.feedback(15)                                                                
"Chin up, Your Name. You'll get it next time!" 

Note the differences

  • In creating an instance of the class the class name is followed by parentheses
  • Don't pass in an argument for self. This is handled behind the scenes

3) I tried the following in a workspace

def feedback(self,grade):
    if grade > 50:
          return(self.praise(self))

and received an error

>>> from file2 import Student                                                      
>>> me = Student()                                                                 
>>> me.feedback(15)                                                                
"Chin up, Your Name. You'll get it next time!"                                     
>>> me.feedback(51)                                                                
Traceback (most recent call last):                                                 
  File "<stdin>", line 1, in <module>                                              
  File "/home/treehouse/workspace/file2.py", line 12, in feedback                  
    return(self.praise(self))                                                      
TypeError: praise() takes 1 positional argument but 2 were given  

Are you sure you passed in a grade over 50 so the code executes?

Hey! KRIS, Thank you for taking the time to help me out!!.

Ok, replying to your points:

1) Answer; I don't know why it accepted my code, as you pointed out, what if the condition is 50, you're absolutely right!. I gave input of 50 and it did not execute anything hence an error. I will fix that.

2) Answer; You just pointed out a critical error in my code, as my input was totally wrong. I gave input of

from file2 import Student

me = Student

# It should have been " me = Student()" with the parentheses, as you pointed out.

I implemented the fixes you described and the results were PERFECT!!!.

KRIS, I have little confusion here, let me describe it with the code below.

This is the code of my script format.

class Student:
    name = "Your Name"

    def praise(self):
        return "You inspire me, {}".format(self.name)

    def reassurance(self):
        return "Chin up, {}. You'll get it next time!".format(self.name)

    def feedback(self,grade):
        if grade > 50:
            return(self.praise(self))

        elif grade < 50:
            return(self.reassurance(self))

This is the code being executed in the Shell.

me = Student
me.feedback(12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: feedback() missing 1 required positional argument: 'grade'
me.feedback(me,12)
"Chin up, Your Name. You'll get it next time!"

Note that in the script format code, the "feedback" method.

def feedback(self,grade):
        if grade > 50:
            return(self.praise(self))

        elif grade < 50:
            return(self.reassurance(self))

I gave "self" arguments in the "if" and "elif" conditions. This was the code that I had a problem with which was then solved. What I don't understand fully is that what is the difference between

me = Student #Without parentheses

me = Student() #With parentheses

The difference I observed is that WITHOUT the parentheses, I had to give in "self" arguments in the "if" and "elif" conditions and WITH parentheses no "self" argument was needed in the "if" and "elif" conditions. (Which is the right code)

KRIS could you please explain what is the difference between "me = Student" and "me = Student()" and as I demonstrated above why "me = Student" input requires a "self" argument?, Could you explain this as well.

Thank you KRIS