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!

9 posts were split to a new topic: Do I need to return grade, or are there other options?

3 posts were split to a new topic: Why can’t I set a global variable within a function?

4 posts were split to a new topic: Why doesn’t this perform every if?

3 posts were split to a new topic: Why is this code incorrect?

4 posts were split to a new topic: Code doesn’t work in reverse?

3 posts were split to a new topic: Syntax error and no starting if?

3 posts were split to a new topic: Misspelled function name

4 posts were merged into an existing topic: Do I need to return grade, or are there other options?

A post was merged into an existing topic: Do I need to return grade, or are there other options?

How cold I print the result of this function? I tried many things and didn’t work

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
  
print(grade_converter(1))

works fine:

image

i don’t see the problem?

Dear reader, please tell me where am I wrong:

Every (if) statement is checking the expression for True or False.

If the expression is True - the block of code after the colon (:slight_smile: is executed.

If the expression is False - the machine evaluates the next if statement and so forth till the (return)

Let’s assume my GPA is 2.1 and that’s why I am writing this…

Here I defined a function:

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"

And here I am defining a variable and assigning it a grade by calling a function (grade_converter) with my 2.1 GPA.

grade = grade_converter(2.1)

Here is what the machine is thinking:

grade_converter(2.1):
if 2.1 >= 4.0: False, check next expression
return “A”
if 2.1 >= 3.0: False, check next expression
return"B"
if 2.1 >= 2.0: True, execute the code and end the function
return “C”
if gpa >= 1.0:
return “D”
else:
return “F”

Here the machine prints my grade “C”, same result as if I used (elif) instead of 2,3 and 4th (if) and I have a question:

If I achieved same results with (if) why do we need (elif)?

In which case would I use (elif)?

Thank you for your time.

Danny

return means the function hands back done data and is done executing.

If you would use print() instead for example, you would get C, D and F when using if only

i would use elif because its more logical, less likely to introduce bug when anther developer takes over and need to make changes

Help me understand why when using if and then print(), we’d get C,D and F. Doesn’t if function work like this: when if condition is met it doesn’t move to next line. Isn’t this true ? Why is C, D and F getting printed then.

that would be true when doing:

if 
elif 
elif 
else

now when if is true, the remaining elif and else clauses are skipped and the code continues after the conditions.

when using if only (and no return), we can get multiple conditions being true.

I think I just had the problem you did – I entered a function that the assessment kept coming back as "didn’t return " on repeatedly, then checked the solution after a long frustrating period of checking my code only to find it was only minorly different.

… then proceeded to modify my code back to exactly what it was beforehand, only to find it worked just fine now.

Bug?

I tried using this code for exercise 11/13 of Control Flow: Else If Statements and it worked.
Not sure, if this is correct.
Why id grade assigned ‘F’ value?

def grade_converter(gpa):

  grade = 'A', 'B', 'C', 'D', 'F'

  if gpa>=4:

    return 'A'

  elif gpa>=3:

    return 'B'

  elif gpa >= 2:

    return 'C'

  elif gpa >= 1:

    return 'D'

  else:

    return 'F'

better question, what is the grade variable doing at all? Doesn’t seem to be doing anything

Hi!
Here is my code for this exercise. I’m just trying out different things.

def grade_converter(gpa):
if (gpa >= 4.0):
print(“A”)
elif (gpa >= 3.0):
print(“B”)
elif (gpa >= 2.0):
print(“C”)
elif (gpa >= 1.0):
print(“D”)
else:
print(“F”)
grade = grade_converter(2.8)

I get the error message “maximum recursion depth exceeded in comparison”. I’ve never seen this error before and I don’t understand why I got this error. Can someone please explain why I get this message and also why my code doesn’t print “C” in the console? Thank you.