Do I need to return grade, or are there other options?

The code below works pretty well. Is there any special need of declaring grade as variable at the beginning as suggested in the exercise?

def grade_converter(gpa):
if gpa >= 4.0:
return “A”
elif gpa >= 3.0:
return “B”
elif gpa >= 2.0:
return “C”
elif gpa >= 1.0:
return “D”
else:
return “F”
grade = grade_converter(0.8)
print(grade)

No special need, no. It is another way of doing it where there is only one return in the function, at the end.

1 Like

There is a very simple reason to store the return value in a local variable. The reason is so that while you debug more complicated code in the future, you can “watch” or “inspect” the variable in your code that causes the variable to change.

This is important, otherwise, when dealing with slightly more complex code, you may find it very difficult to determine which statement is returning the value, etc.

The second reason, is that it is a good idea to try if possible to only have one return statement per function. This makes debugging and following your code much easier for others, or even for yourself three years after you wrote it!

I’m wondering if there’s something more I(we) can do to improve this code base on past lessons (Python syntax and learn python functions)?

def grade_converter(gpa):
grade = ‘’

if type(gpa) == int or type(gpa) == float:

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

else:
return ‘Something wrong with input’

print(grade_converter(‘swqefd’))
print(grade_converter(7))
print(grade_converter(3.9))
print(grade_converter(2))
print(grade_converter(1.1))
print(grade_converter(0.6))

We are encouraged as learners and seasoned developers to stretch the envelope and learn or devise as many different approaches as we can. Some of those attempts may seem strange and unusual…

>>> def grade_converter(gpa):
	try:
		if int(gpa) in range(5):
			return ['F', 'D', 'C', 'B', 'A'][int(gpa)]
	except:
		return "Input Error!"
	return "Input not in range"

>>> grade_converter(-1)
'Input not in range'
>>> grade_converter('a')
'Input Error!'
>>> grade_converter(5)
'Input not in range'
>>> grade_converter(4)
'A'
>>> 

That’s not to suggest that this is a better approach, only a different one that gives the expected results. I’ve always said that there are one hundred ways to cook an egg. We’re not going to learn all of them in a day. Programming is the same. It’s a day by day pursuit, and we only get results when we are willing to get our hands dirty.

Bottom line, don’t throw out the baby with the bath water. First we write code that works, then we try to break it, then we fix it so it cannot be broken, then we work at refactoring, rinse, and repeat.

2 Likes

Now I did it without adding the variable Grade because I got ahead of myself without reading all of step 1. I finished the code and it executed to perfection. It wasn’t until I finished reading step 1 that I saw the part of the grade. This is what my working code came out to.

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

For this following code, that correct code that I checked by hitting view solutions.

My question, the instruction asked us to return a variable called “grade” . I get it, but in the solution provided - also - shown - below, there is not “return” used…so m confused!

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"

return grade

There is a return statement after the if statement. grade is returned.

I didn’t understand why I have the value of the first variable that we have to create (grade) shoud be called “F”. Can someone help me, please?

I knew how to do all the rest of the exercise but maybe that was the easiest part and I could think about it…

Should I study programming logic?

I’d suggest breaking it down into smaller steps. In many cases it’s possible to think of a solution but the translation to actual code is the difficult bit. If you can follow what each line does then you can follow the logic behind that code.

Start from the top of that function and just go line by line. What happens if grade is greater than 4.0. Carry on from there to the end of the function. What is the expected output. Do this for each of the if/elif statements (e.g. what if grade is 3.5, what about 2.5 and finally 1.5). What happens if none of them evaluate to True?

Looking at a block of code can be daunting but there is always a flow to follow. Take it line by line with a pen and paper if that helps and just work out what would happen on that line. If you can do this you can always trace the flow.

according to what the earlier instructions said, if you input a gpa of 4.0, you would receive a message that said A, B, C, D, F because a gpa of 4.0 will satisfy all of the if statements. Hope that helps

I wanted to solve for an unexpected number and used the else statement to capture an unexpected number, such as -3.2. I’ll have to figure out how only allow integers because when I entered ‘A’, I received an error.

def grade_converter(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"
  elif gpa >= 0.0:
    grade = "F"
    return grade
  else:
    print("Please enter a valid number")

‘A’ is type str, so that input will raise an exception. gpa needs to be a number.