Codecademy Forums

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.

2 Likes
  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

Hello, I’ve got a question regarding the exercise:

  1. what is the point of assigning the return value to a variable called grade when I can get the same return value without using it?
  2. what is the point of using elif statement here when I can get the same result just by using if-return?
    I’m just thinking about the simplicity of the code… Thank you!!
    My code:

def grade_converter(gpa):
if gpa >= 4.0:
return “A”
if gpa >= 3.0:
return “B”
if gpa >= 2.0:
return “C”
if gpa >= 1.0:
return “D”
if gpa >= 0.0:
return “F”

When assignment is used, we need elif, but when return is used, we don’t. It depends which school of thought we’re from. Some would have only a single return in the function, after the if statement. Some are okay with multiple returns in a function. It’s a subjective argument.

OK, thank you very much.

1 Like

Some nested ternary silliness…

>>> grade = 79
>>> def letter_grade(x):
	return 'F' if x < 60 else 'D' if x < 70 else 'C' if x < 80 else 'B' if x < 90 else 'A'

>>> letter_grade(grade)
'C'
>>> 
2 Likes