Else If Statements


In this exercise, I did my code differently by just checking if the gpa and returning the grade.
But when I saw how system did it, i was confused. Why did it use "grade = “F” for? and can you also explain at then end what is return grade doing?

def grade_converter(gpa):
  grade = "F"
  if gpa >= 4.0:
    grade = "A"
  elif gpa >= 3.0:
    grade = "B"
  elif gpa >= 2.0:
    grade = "C"
  elif gpa >= 1.0:
    grade = "D"
  return grade


You create a default value to fall back on when you do it this way with “F”. That way if none of the conditions are met you just return the variable.

Got it but why did we create variable grade? I am just confused why it was used?

So we print the letter grade according to the score. This is how I did it, which is different.

def grade_converter(grade):
    if grade >= 90:
        return "A"
    elif grade >= 80 and grade <= 89:
        return "B"
    elif grade >= 70 and grade <= 79:
        return "C"
    elif grade >= 65 and grade <= 69:
        return "D"
        return "F"

# This should print an "A"

# This should print a "C"

# This should print an "F"

So there could be a singular return statement, rather than multiple. Some would have it that way for greater clarity. There are discussions relating to multiple returns from a function if you are willing to dig around the SERPs.

1 Like

btw you already know it’s less than or equal to 89/79/69 from previous tests (unless non-integers are used)

1 Like