How to check for passing in the Grade class?

Question

In this exercise, the optional is_passing() method is supposed to check if the score is passing? How can that be implemented?

Answer

The is_passing() method needs to check the object’s self.score attribute against the class variable Grade.mimimum_passing to determine if the score meets the requirements. If the score is greater than or equal to the minimum passing value, the method should return a value of True. Otherwise, it should return a False.

6 Likes

Is the following code correct or does it have some redundant element?

class Grade:
  minimum_passing = 65
  def __init__(self,score):
    self.score = score
  def is_passing(self,score):
    self.score = score
    if self.score>=Grade.minimum_passing:
      return True
    else:
      return False
1 Like

here:

def is_passing(self,score):
    self.score = score

the score parameter is redundant, and the second line is completely redundant. You already made score available through self in the init method. No need to do it again

1 Like
class Grade:
    minimum_passing = 65
    def __init__(self,score):
        self.score = score
    def is_passing(self):
        if self.score >= minimum_passing:
            return True
        else:
            return False

This should work, or is better to include the Grade. minimum_passing in the if-statement?

1 Like

It will not work like that.

You’ll need to use either Grade.minimum_passing or self.minimum_passing. Both will work. (Personally, I prefer Grade.minimum_passing, as it seems more logical to me.)

2 Likes
class Student:
  def __init__(self, name, year):
    self.name = name
    self.year = year
    self.grades = []
  def add_grade(self, grade):
    if type(grade) == Grade:                     #<--- **here**
      self.grades.append(grade)
    else:
      return


I don’t understand this point. Why expression with Grade at this point didn’t raise any Error?
Why word Grade can be called in another class (Student class) have no relationship with it?
Can someone explain it to me?
Thank a lot!

The link which is posted in this topic doesn’t lead me to the exercise, could you give the exercise url?

What i think is happening (but seeing the exercise would really help, can’t remember them all) is the following:

my_student = Student("example", "first")
my_grade = Grade(60) 
my_student.add_grade(my_grade)

so you pass an instance of another class to this method

then checking you get the right class instance is a good thing to do.

Here is the code of exercise.

	class Student:
	  def __init__(self, name, year):
	    self.name = name
	    self.year = year
	    self.grades = []
	  def add_grade(self, grade):
	    if type(grade) == Grade:         #<-- here
	      self.grades.append(grade)
	    else:
	      return
	  def get_average(self):
	    s = 0
	    for student in self.grades:
	      s += student.score
	    return s/len(self.grades)
	roger = Student("Roger van der Weyden",10)    
	sandro = Student("Sandro Botticelli",12)
	pieter = Student("Pieter Bruegel the Elder",8)
	class Grade:
	  minimum_passing = 65
	  def __init__(self, score):
	    self.score = score
	  def is_passing(self):
	    if self.score >= Grade.minimum_passing:
	      return True
	    return False
	x = Grade(100)    
	pieter.add_grade(x)

I lost url for this exercise. But the exercise I’ve mention is the exercise of this topic!
Thanks @stetim94

Seems my guess was a reasonable one? you could pass anything to add_grade method:

pieter.add_grade(100)
pieter.add_grade("hundred")

so a sanity check (if its indeed a instance of Grade class) seems good.

i am struggling what you mean by this. I don’t any reason for an error? At most, the comparison is false.

you use type() to check if grade parameter is instance of Grade class.

1 Like

yep, that’s what I mean.

The word Grade in this line. What exactly is it?

Can I call it anywhere in the module(without () )?

Grade is the class:

class Grade:

a instance of a class (pieter) has the class (Grade) as its type.

Yes, Grade class is in the global name scope. Using the parentheses would create a new class instance

2 Likes

@stetim94 Thank you so much!

Hi, I’m wondering what Grade(100) actually does.
I wrote the following code:

pieter.add_grade(Grade(100))
print(pieter.grades)

And it prints out:
[<main.Grade object at 0x7f7e59c15ba8>]

Should I write a repre in class Grade?
As I understood, repre just changes how it is shown to the outside, but not change the content itself? So what exactly was stored in pieter.grades after add.grade? Is it just the number 100?

if you want, you can do this

an instance of the Grade class. This instance has a score instance variable so you could do:

pieter.grades.score

the advantage of this approach is that we can do:

pieter.grade.is_passing()

to check if pieter passed the test.

I think I got it! Thank you!

It’s still not clear to me, sorry.
What do I have to type to check if it’s passing ?
pieter.grades.is_passing - not working I I tried many other ways and can’t seem to find a solution.
Please HELP

class Student:
    def __init__(self, name, year):
        self.name = name
        self.year = year
        self.grades = []

    def add_grade(self, grade):
        if type(grade) == Grade:
            self.grades.append(grade.score)

    def get_average(self):
        avg = 0
        return sum()


class Grade:
    minimum_passing = 65

    def __init__(self, score):
        self.score = score

    def is_passing(self):
        if self.score >= Grade.minimum_passing:
            return True
        else:
            return False


# execute
roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)
x = Grade(60)
pieter.add_grade(x)
pieter.add_grade(Grade(77))

print(pieter.get_average)
pieter.grades.is_passing

is_passing is a method, how do we call a function/method?

1 Like

I was missing THE MOST BASIC thing -> () sigh… Thank you.

After writing the is_passing method, how do we use it on one of the students we made to see if they’re passing?

class Student:
  def __init__(self, name, year):
    self.name = name
    self.year = year
    self.grades = []
  
  def add_grade(self, grade):
    if type(grade) == Grade:
      self.grades.append(grade)
class Grade:
  minimum_passing = 65
  def __init__(self, score):
    self.score = score

  def is_passing(self):
    if self.score >= Grade.minimum_passing:
      return True
    else:
      return False

roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)
pieter.add_grade(Grade(100))
x = Grade(70)
print(x.is_passing())
print(pieter.grades.is_passing())

print(x.is_passing()) works fine.
The line after it doesn’t work, and I’m not sure how to write to check if Pieter is passing. He has a Grade of 100, so shouldn’t there be a way to check that?