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 trialhaakon Guttormsen
2,157 PointsHow to take an argument within a method, and to make an if-statement that returns one out of to other methods in class.
I want to take an argument 'grade' (am I doing it wrong?) and return one out of to other methods within the class based on the value of grade. Can anyone see what I'm doing wrong here? thanks for any help!
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=int(input('what is your grade? ')
if grade>50:
praise(name)
elif grade<50:
reassurance(name)
1 Answer
Øyvind Andreassen
16,840 PointsThe challenge isn't asking you to provide a solution to take the input, but rather saying that feedback, should take an argument. So you the first line of the function is redundant.
It also ask you to run praise
if the grade is over 50, and reassurance
if it's 50 or below. So your if-statment should take this into consideration.
Also, since praise
and reassurance
are methods of the Student-object you use self.method() to call them.
class Student:
name = "My 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()
return self.reassurance()
The way you would use the feedback function would be something like:
student = Student()
student.feedback(54) #You could either print the return statement, or save it to a variable
Of topic, but one typo in your code that will cause problems.
grade=int(input('what is your grade? ')
You are missing a closing parentheses to close out int(), easy thing to miss.
haakon Guttormsen
2,157 Pointshaakon Guttormsen
2,157 PointsThanks Øyvind. I was not sure what "taking an argument" meant but now it's clear.