Do I need to return grade, or are there other options?

Hi! I just started learning Python 3 and i have a question about ‘Else If Statements’ exercise 11:

Calvin Coolidge’s Cool College has noticed that students prefer to get letter grades over GPA numbers. They want you to write a function called grade_converter that converts an inputted GPA into the appropriate letter grade. Your function should be named grade_converter , take the input gpa , and convert the following GPAs:

  • 4.0 or higher should return "A"
  • 3.0 or higher should return "B"
  • 2.0 or higher should return "C"
  • 1.0 or higher should return "D"
  • 0.0 or higher should return "F"

You should do this by creating a variable called grade that is first set to "F" .

Then, you should use elif statements to set grade to the appropriate letter grade for the gpa entered.

At the end of the function, return grade .

The last part suggests a certain way of doing it but i started with another method that seemed to work aswell. Are both alright or should i use either one?

method 1:

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

Method 2:

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"
  else:
    grade= "F"
  return grade

I thought maybe both are correct but one is preferred whenever we encounter more complex codes, so for future laziness, hope someone can enlighten me:) Thank you! :hugs:

6 Likes

on the second method, after the “else”, i just wrote “return grade”. If nothing above is True, the value of grade is already “F”

1 Like

Good call. I had the same question as OP, but I guess your method saves a step. Thanks!

1 Like

As we can see, there are enough cases to satisfy the inputs without having to set grade at the start of the function. else: handles all the outside cases.

if ...:
    grade = 'A'
elif...:
elif...:
elif...:
else:
    grade = 'F'
return grade

Note that this is fully reversible…

in reverse order
if gpa < 1: return 'F'
if gpa < 2: return 'D'
if gpa < 3: return 'C'
if gpa < 4: return 'B'
return 'A'
1 Like

I had a similar question. I’m not sure why it was suggested to create a variable called grade. It wasn’t needed.

Thanks.

I wrote my function the same way you did (Method 1) and was also wondering if there’s an advantage of one over the other.

I have the exact code as you have in method 2. Except I think that there may be an issue on codecademy’s end because it will not let me click on Next to go on! I tried everything on my own and then got their solution because I thought I couldn’t figure it out. However, I was ok and it still won’t let me go on. Ideas?

Thanks,

Seems to be working. I clicked “Reset”, pasted in “Method 2”, clicked “Run” and “Next” lit right up!

Thank you. On a whim I click on the refresh button and then it worked. I’m not sure why, but sometimes the website gets a little stuck I guess.

1 Like

Hello, I’ve got a question regarding the exercise:

  1. what is the point of assigning the return value to a variable called grade when I can get the same return value without using it?
  2. what is the point of using elif statement here when I can get the same result just by using if-return?
    I’m just thinking about the simplicity of the code… Thank you!!
    My code:

def grade_converter(gpa):
if gpa >= 4.0:
return “A”
if gpa >= 3.0:
return “B”
if gpa >= 2.0:
return “C”
if gpa >= 1.0:
return “D”
if gpa >= 0.0:
return “F”

When assignment is used, we need elif, but when return is used, we don’t. It depends which school of thought we’re from. Some would have only a single return in the function, after the if statement. Some are okay with multiple returns in a function. It’s a subjective argument.

OK, thank you very much.

1 Like

Some nested ternary silliness…

>>> grade = 79
>>> def letter_grade(x):
	return 'F' if x < 60 else 'D' if x < 70 else 'C' if x < 80 else 'B' if x < 90 else 'A'

>>> letter_grade(grade)
'C'
>>> 
2 Likes

I was wandering why we should create the variable grade and why the if/elif method is not preferred so I broke it made some research.
Lets suppose that the user decided to enter as an input -1 and not what we are expected to see logically and inside our terms (from 0 to inf). The assigned variable method will return F whatever the wrong input can be, so it is like bug solving.
I find it the same as the variable method to add an else at the end of the function saying whatever else the input can be you will get an F.

… so I find these two methods equally efficient:

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

def grade_converter_2(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

(feel free to reply if you find something wrong)

1 Like

Hi, @linguistic8184686867 - It’s good to see someone actually having fun with coding! Does one way seem more elegant than the other to you?

Now, since you are interested in playing around a bit with these exercises, make a note to yourself: After you have covered the lessons on Python dictionaries, return to this one and see if you can’t figure out how to get rid of that entire if-elif chain!

1 Like