FAQ: Control Flow - Else If Statements

This tells us to look for calls to the function from within the code block. That’s what recursion is.

From the evidence in the error message we can assume that the last line is indented.

Here is an example of recursion…

>>> def factorial(n):
    return n if n == 1 else n * factorial(n - 1)

>>> factorial(7)

Notice the function is called from within its code block? That makes this a recursive function.

Yes, you are right. Last line of my code was indented. The example is quite helpful. It makes so much more sense now. Thank you so much, sir!!

1 Like

You’re welcome!

Recursion will come up later in the course so don’t divert your attention, just now. This way you will recognize the error and be able to apply it to your current circumstance. Python is very dependent upon indentation clues to distinguish scope. Don’t depend on the editor. Do your own deliberate indentations to represent the scope you intend. (Coding tip)

Sounds good. Thank you for the advise.

1 Like

Hi guys,

So, in this example, you are asked to create a variable called grade, in the solution that codecademy gives me it assigns “F” to the grade variable. Is there a reason why he assigns this letter specifically, or can it be left blank somehow so we then call the variable we desire later on?

Pls help :slight_smile:

The initial definition is the default return value. Notice it (‘F’) is not assigned anywhere in the if statement?

I dont know understand what im doing wrong here

grade = “A”,“B”,“C”,“D”,“F”

def grade_converter(gpa):

if gpa >= 4.0:

return grade == "A"

elif gpa >= 3.0:

return grade == "B"

elif gpa >= 2.0:

return grade == "C"

elif gpa >= 1.0:

return grade == "D"

if gpa >= 0.0:

return grade == "F"

return grade

keep on getting error saying " It looks like your function didn’t convert the grades correctly. 4.1 should return the letter grade A, but instead it returned False."

== is a comparison operator, the result of a comparison is a Boolean value. Why would you want to use comparison here?

Hello team,

Recent joiner and first-time poster, so please bear with me and let me know if I’m not posting in the correct place !

I’m having an issue with the exercise [Else If statements]


My code below:
def grade_converter(gpa):
grade = gpa
if gpa >= 4.0:
elif gpa >= 3.0:
print (“B”)
elif gpa >= 2.0:
print (“C”)
elif gpa >= 1.0:
print (“D”)
elif gpa >= 0.0:
return grade


When running the code, the output is technically correct as it prints out the correct grade letter depending on the input value (“A” in this example) but it also prints out the actual GPA value from my argument (here 4.1), as below

I also gets this message when running the code:
It looks like your function didn’t convert the grades correctly. 4.1 should return the letter grade A, but instead it returned 4.1.

Could someone explain what I’m doing wrong ? The solution starts by defining the variable grade as “F”, which I don’t understand…

Thanks for your help !


That gives an initial value which if unchanged by the final return statement will be the default return value.

The problem with the above code is the value never changes since the grades are only printed, not returned. Change all those print statements to,

grade = "A"
grade = "D"

Now the values will be changed (or not) before the return statement.

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”
elif gpa >= 0.0:
return ‘F’


in the example question with donations that goes along with the elif exercise it states that if the elif statements were just if, then it would print all the possible return statements, which it does

But why when I change all of the elif statements in the code above, it still returns just one answer?


there is a difference between print and assigning to a variable. In the latter case, the variable just gets overwritten every time, still resulting in a single value. print will print each time

the ultimate test would be to combine the exercise with the example question (have assignment and print at each if clause.

grade = 86

if grade >= 90:
elif grade >= 80:
  elif grade >= 70:
    elif grade >= 60:

May I know why I still get error even I have followed the instruction & example?

This is the error:

  File "script.py", line 7
    elif grade >= 70:
SyntaxError: invalid syntax

elif has to be after an if or another elif, this elif is nested:

  elif grade >= 70:

as are all elifs and else after it. That doesn’t work.

1 Like

8 posts were split to a new topic: Control flow - Else if statementss

Why is this considered incorrect?

grade = 86 if grade >= 90: print("A") elif grade >= 80 and grade < 90: print("B") elif grade >= 70 and grade < 80: print("C") elif grade >= 60 and grade < 70: print("D") else: print("F")

If you do not include the “and” statement and the order of checks is different from “top to bottom”, would it not result in printing multiple responses? You need the “and” statements to include an upper bound on the grade classification or else a grade of 86 would meet multiple criteria (i.e. 86 is greater than 80 as well as 70, 60, etc), right?

Edit: I just checked it by changing the order of “if” checks without the “and” statement, and the “correct” code will produce an incorrect answer.

grade = 86 if grade >= 90: print("A") elif grade >= 70: print("C") elif grade >= 80: print("B") elif grade >= 60: print("D") else: print("F")

Here, the B and C check are reverse in order and since 86 is greater than 70, it classifies an 86 as a C, which is incorrect.

The problem with this exercise is that their solution depends upon the user listing their “if” statements from A down to F.

1 Like

Top down or bottom up won’t matter so long as the sequence is maintained. We cannot get to the fourth floor without first going to the lower three floors, in order. Nor can we get to the first floor without going through the floors above it.

There is no required extra logic so long as the sequence order is ascending or descending.

if x < 60: # F
if x < 70: # D
if x < 80: # C
if x < 90: # B
# A

The cascade gives us a process of elimination.

Is it a good idea though to assume the user writes the sequence in ascending or descending order though?

The user isn’t writing the code, we are. Code is written using the best reasoning of the programmer. So long as the relational operators are correctly chosen, and there are no assumptions, the language promises to interpret our code the way we write it.

When do we use the nested if statements and when do we use if, elif, else? what is the difference?