# 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):
if gpa >=4.0:
elif gpa >=3.0:
elif gpa >=2.0:
elif gpa >=1.0:
else:
``````

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!

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 ...:
elif...:
elif...:
elif...:
else:
``````

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:

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
return 'F' if x < 60 else 'D' if x < 70 else 'C' if x < 80 else 'B' if x < 90 else 'A'

'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"

if gpa >= 4.0:
elif gpa >= 3.0:
elif gpa >= 2.0:
elif gpa >= 1.0: