FAQ: Control Flow - Else If Statements

Can anyone tell me why I would be getting a snytax error on line 2 with the following code? I wrote the “def grad_coverter(gpa):” function and hit enter and checked it several times…

def grade_converter(gpa):
  elif gpa >= 4.0:
    return grade = "A"
  elif(gpa>=3.0):
    return grade = "B"
  elif(gpa>=2):
    return grade = "C"
  elif(gpa>=1):
    return grade = "D"
  elif(gpa>=0):
    return grade = "F"
  return grade

The first in the chain must be if ; only after if can you start with the elifs.

1 Like
  File "main.py", line 20
    return grade = "A"
                 ^
SyntaxError: invalid syntax

The error message is hinting that assignments are not proper syntax in return statements.

return %value%

where %value% can be any expression.

1 Like

I want to ask about my function, is there any mistake when I type like this?

def grade_conventer(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”
else:
grade = “F”
return grade

results = grade_conventer(4.0)
print(results)

Why does the output always say " Did you change the name of function grade_converter? "

You apparently didn’t spell the function name the way it was suggested in the assignment.

Thank you for your help :slight_smile:

1 Like

I wrote my function the same way you did (Method 1) and was also wondering if there’s an advantage of one over the other.

I have the exact code as you have in method 2. Except I think that there may be an issue on codecademy’s end because it will not let me click on Next to go on! I tried everything on my own and then got their solution because I thought I couldn’t figure it out. However, I was ok and it still won’t let me go on. Ideas?

Thanks,

Seems to be working. I clicked “Reset”, pasted in “Method 2”, clicked “Run” and “Next” lit right up!

Thank you. On a whim I click on the refresh button and then it worked. I’m not sure why, but sometimes the website gets a little stuck I guess.

1 Like