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

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