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.


#12

so I used without adding the else at the end.
I read in one of the python books that else statement is optional and you can do without it if not so necessary. what could be the shortfalls of my method

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”