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.