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

You are welcome, @janosh, and thank you, @milesmccracken. :smile:

LOL :smile: :smile: :smile: :smile: :rofl: :rofl: :rofl: :rofl: :rofl: :joy: :joy: :joy:

2 Likes

Method 3:

grade = ""

def grade_converter(gpa):
  if gpa >= 4.0:
    grade = "A"
    return grade
  elif gpa >= 3.0:
    grade = "B"
    return grade
  elif gpa >=2.0:
    grade = "C"
    return grade
  elif gpa >= 1.0:
    grade = "D"
    return "D"
  else:
    grade = "F"
    return grade
  
print(grade_converter(2.4))
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.

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”

grade = grade_converter
print(grade(2.0))

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.

def grade_converter(gpa):
if gpa >= 4:
grade = β€œA”
print(grade)
return grade
if gpa >= 3:
grade = β€œB”
print(grade)
if gpa >= 2:
grade = β€œC”
print(grade)
if gpa >= 1:
grade = β€œD”
print(grade)
else:
grade = β€œF”
print(grade)

grade_converter(3.6)

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

def grade_converter(gpa):

if gpa >= 4:

grade = "A"

return grade

if gpa >= 3:

grade = "B"

return grade

if gpa >= 2:

grade = "C"

return grade

if gpa >= 1:

grade = "D"

return grade

if gpa >= 0:

grade = "F"

return grade

print(grade_converter(3.5))

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:

def grade_converter(gpa):
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
print(grade_converter(3.7))

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”?

def grade_converter(gpa):
grade = β€œβ€
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

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:

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"

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

def grade_converter(gpa):

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”

print(grade_converter(10));

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 ifs as they are. Now run the code.