FAQ: Control Flow - Else If Statements


#1

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!


#2

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:


#3

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


#4

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


#5

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'

#6

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


#10

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


#11

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.


#14

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))


#15

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.


#16

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.


#17

gotcha! Thanks for the reply!


#18

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!


#19

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.