Why is this code incorrect?

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”
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!

1 Like

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.

1 Like

Thank you for the help mtf!

1 Like

I got yet ANOTHER bug saying my code returned a NameError despite the code working and printing the correct value when I tested it.

Hi @clovenhoof101,

What does your function do if gpa is 0.5?

Hi there,

Yes I hadn’t typed else for F but my point remains it said there was a name error when there wasn’t one.

Hi @clovenhoof101. The previous point still stands. What are you returning when gpa is, for example, 0.5? Use that code with @appylpye’s suggestion and you’ll find an error.

1 Like

Not really, because I wasn’t looking for that value in particular when I ran the code, it worked fine and didn’t return a name error.

A number of the lessons test a number of scenarios silently when you attempt to submit the code. This is mainly to ensure that the requirements specified fo that lesson have been met. I assume it’s one of these silent tests that threw a name error.

You could certainly test it with a value that you know will perform as expected but that doesn’t guarantee it meets all the requirements.


Ah okay, that makes a lot of sense because it was referring to the silent tests you mentioned and I was getting very confused and frustrated. Will note this going forward and make sure the code is complete for any of these potential cases.


When testing code, include a wide range of cases so that you thoroughly check for all possible bugs, even if Codecademy does not supply those cases explicitly in the editor window or within the instructions. In the current case, that would entail including gpa numbers that correspond to all possible letter grades. This strategy of testing applies within the Codecademy instructional content as well as in any work environment.