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

You are welcome, @janosh, and thank you, @milesmccracken. LOL            2 Likes

Method 3:

``````grade = ""

if gpa >= 4.0:
elif gpa >= 3.0:
elif gpa >=2.0:
elif gpa >= 1.0:
return "D"
else:

``````
1 Like

Is there a point to this declaration?

1 Like

Can you explain how this code works ?

nice explanation thanks for the help

We start with the lowest grade, ‘F’. If `x` is less than 60 then that is the return. If 60 or greater then we check if it is less than 70. If it is then ‘D’ is returned, and so on up to ‘A’, the default when all the other cases fail.

In total, this represents an expression, not an if statement which is why we can return it.

``````if x < 60:
return 'F'
elif x < 70:
return 'D'
#...
else:
return 'A'
``````

Above is an if statement. Note the difference.

1 Like

I really like the first method because is more simple, but as you know they mentioned a variable called grade, and beginners like me wont never do it without the variable. I went around few hours trying to solve this problem by myself, but couldnt do it. Thanks man.

I tried with print and never worked, now i see you close the variable empty at the botton, something i didnt tried. Great.

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”

I just created the grade variable outside of the function but was wondering if this is a “bad” way of coding and if i should have done it inside the function.

No, it is not ‘bad’, but is there a special reason for doing it? What you have done is create a second reference to the function. That is perfectly valid, especially if the function has a long name and it is called a lot from different places in the code, such as inside loops.

``````def long_function_name_for_clarity():
pass

fn = long_function_name_for_clarity

fn()
``````

Now we can call the function with the simpler reference.

And, yes, it should be declared outside of the function. Would we give a function a new reference from within the function? I can’t find a reason why we would.

I was doing this exercise as well and after it worked out I purposefully tried using if’s and saw that if I do it this way I get multiple conditions being met.

if gpa >= 4:
if gpa >= 3:
if gpa >= 2:
if gpa >= 1:
else:

However, if I do it this way I only get one condition being met even if I use ifs!!

if gpa >= 4:

``````grade = "A"

``````

if gpa >= 3:

``````grade = "B"

``````

if gpa >= 2:

``````grade = "C"

``````

if gpa >= 1:

``````grade = "D"

``````

if gpa >= 0:

``````grade = "F"

``````

Why is that happening?

What does `return grade` do?

This seems to work any of the ways mentioned in this thread, but here is another fairly efficient way to write this:

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

My code was similar to method 2, but instead I did this. Would my method be efficient or is it better to assign the grade variable “F”?

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

We wouldn’t be concerned about efficiency for something like this. It is just a conditional branch. It’s when we get into loops that efficiency comes into question, especially with nested loops. Even still, at this stage of learning we do not need to consider efficiency. That will come up down the road.

Hello! My situation is even weirder!

I wrote the code without even specifying a “grade” variable, but it turned out to work anyway!

Here’s my code:

if gpa >= 4.0:

``````return "A"
``````

elif gpa >= 3.0:

``````return "B"
``````

elif gpa >= 2.0:

``````return "C"
``````

elif gpa >= 1.0:

``````return "D"
``````

elif gpa >= 0.0:

``````return "F"
``````

else:

``````return grade
``````

Does anyone know the reason why “grade” has been accepted even if it hasn’t been defined before?

Thanks, Luca

Hi @py7683618051. This is because the statement `return grade` is never actually reached. Because Python has a fairly broad scope it won’t necessarily throw an error when executed as you might expect it to in some other languages. Under reasonable circumstances gpa is never less than zero. For robust error checking this is an issue but for the given example it should be a problem.

What happens in your console when you call this function with an argument that is less than zero?

there is no difference between if and elif in this code when I tried to print it to the terminal

if gpa >= 4.0 :

return “A”

if gpa >= 3 :

return “B”

if gpa >= 2 :

return “C”

if gpa >= 1 :

return “D”

if gpa >= 0 :

return “F”

Just for s&g, before the first `if`, insert,

``````letter = ''
``````

Now at the end of the function insert,

``````return letter
``````

Then, change all the returns to,

``````letter = 'A'
``````

thru,

``````letter = 'F'
``````

Be sure to leave all the `if`s as they are. Now run the code.