FAQ: Control Flow - Else If Statements

This community-built FAQ covers the “Else If Statements” exercise from the lesson “Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Else If Statements

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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:

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

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

Why can’t I define “grade = F” outside the function?

Here is my code:

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

If my input is greater than or equal to 1.0, then the result will be okay. But, if my input is less than 1, there will be the error like:

Traceback (most recent call last):
File “script.py”, line 13, in
print(grade_converter(0.8))
File “script.py”, line 11, in grade_converter
return grade
UnboundLocalError: local variable ‘grade’ referenced before assignment

Defining grade inside the function works well, but if I define grade before defining the function, I get a name error. Why?

Global variables can be accessed from within functions, but we cannot set them without explicitly naming them as global inside the function.

foo = 0
def bar():
  global foo
  foo += 1

That may have some bearing on your case.

1 Like

I test a code like this, and the result came out as “B”. I was wondering why result shows only as “B”, instead of “B C D E F” since each if statement is independent, and it meets gpa>3.0, gpa>2.0, gpa>1.0,gpa>0.0 conditions? Please help
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”
else:
return “F”
print(grade_converter(3.0))

I believe this is because you are using the “return” statement multiple times in your function.

To my knowledge, (bear with me as I’m also a beginner) once the “return” statement is passed, the script immediately ends execution of the current function, which is probably why “C”, “D”, and “F” aren’t appearing.

If you were to remove those extra returns and set grade = “A”, grade = “B”… grade = “F” and at the end function “return grade”, you should be able to achieve the multiple grade outputs.

2 Likes

That is exactly the result to expect. We only want a single letter return, not a list of letters. What would give one that idea, and then write this code? We code toward the outcome we desire, without guessing. We are at the controls. A function to output a list of letters would not have to go to the length we have here.

gotcha! Thanks for the reply!

So I did this as my code and it said it was incorrect:
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

When I had pulled their solution they initially set grade = “F” and that really confused me. So if you set a variable in the beginning of the function and then use booleans after that, they can override the value of the variable? Also, is my version correct? Did they just lose the last step I wrote by initially setting grade = “F”?

Any help is appreciated! Thank you!

The order in which we go through the gpa’s, low to high or high to low is of no importance, so long as the code (and logic) is correct. grade is set in every case so does not need an initial value.

grade = ""

can be removed with no effect. Now if you were to set it to "F" at the start then the else clause would not be necessary.

There are any number of ways to approach this problem although the SCT may have a limited expectation which might explain why your answer was not accepted. Be sure your syntax is correct. Glean what you can from the their solution, reset the lesson and start again from scratch using what you learned from their example.

1 Like

Thank you for the help mtf!

1 Like

So I was testing to see if it can be done this way. I thought the logic of the following is sound but it won’t return anything but “B”. The “solution” had grade set to F.
I wanted to if I could do it in reverse. Where did I screw up:

def grade_converter(gpa):
grade = “A”

if gpa < 4.0:
grade = “B”
elif gpa < 3.0:
grade = “C”
elif gpa < 2.0:
grade = “D”
elif gpa < 1.0:
grade = “F”

return grade

print(grade_converter(2.7))

As always, follow the code:
You have set ‘grade’ to be 2.7.
The first “if” returns True, since, indeed, 2.7 < 4.0 is True

Now, it is the essence of an “if - elif” sequence that once one of them returns True, all of the rest are skipped. That is how ‘elif’ works.

So, once you have True in the first ‘if’, the ‘if’ block is executed, the variable grade is set to “B”, and control skips every ‘elif’ (and the ‘else’, should one be there), and drops straight to the return statement.

Now, consider: Let’s follow the code, again, but suppose that you had order of the grades reversed, the first line being

if gpa < 1.0:
    grade = 'F'

… ?

This makes so much sense.
Thank you for a great response and now I see why it cannot be written the way I wanted. The check from top down needs to be staggered so as not to stop the rest.

So if we did what you propose, the 2.7 that I entered would not return your if gpa<1 true and would go to the next until it is triggered true (please correct me if I have it wrong):

gpa < 1 would return False
gpa < 2 would return False
gpa < 3 would return True and therefore return “C”.

Yes, precisely! :slightly_smiling_face:

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

Thanks.