# The big if (I'm stuck on that)

Hello earthlings!
I got a problem with this exercise, I’ve been trying a lot of different codes and combinations, but it always says that the letter should be another than the one I’m getting as an answer.

Instructions:
In the workspace to your right, there is the outline of a function called grade_converter().

The purpose of this function is to take a number grade (1-100), defined by the variable grade, and to return the appropriate letter grade (A, B, C, D, or F).

Your task is to complete the function by creating appropriate if and elif statements that will compare the input grade with a number and then return a letter grade.

90 or higher should get an “A”
80 - 89 should get a “B”
70 - 79 should get a “C”
65 - 69 should get a “D”
Anything below a 65 should receive an “F”

Hint:

Be careful with your indentation—grade_converter() is a function (which we’ll get to in the next unit), and as you can see from the comment, function blocks are indented the same way if, elif, and else blocks are.

If you are having trouble creating the if and elif statements, here’s the first one you can use as an example:

return “A”
This will compare the variable grade with the value 90 and if it is greater than or equal to 90 it will return the letter A.

if you look at the output, we see `B` grade being for 70. That can’t be right, so something is going wrong with your comparison. lets look at it:

``````a <= 89
``````

so when `a=70`, this condition is true, 70 is less then 89. But this means B will be returned

1 Like

To go along with the previous reply, keep all your comparisons in the same direction. If you start with `>=` then stick with that operator the whole way.

1 Like

I’m trying to follow the answers but It still says that I just got
A
B
B
When I actually need to obtain
A
C
F

I was trying to figure it out according to the examples and I got this answer which worked!

return "A"
return "B"
return "C"
return "D"
else:
return “F”

# This should print an “F”

As I had never been instructed to these kind of subject, the problems with commas, parentheses and etc were causing me a headache haha.

Thank you so much for all the help <o/

1 Like
``````def get_letter_grade(grade):
return "A"
return "B"
return "C"
return "D"
else:
return “F”
``````

The above relies on fall through to pick up the most valid case. Less than 90 will fall to the elif, less than 80 will continue to fall, less than 70 fall through again, until finally less than 60 falls to the default.

This is what I meant by keeping all the comparisons in the same direction. The logic is simpler to follow and the outcome is what we would expect.

1 Like

My code to give you a different perspective. I blurred the answer because their is people who like to get clues versus the answer. Hope this helps!

``````# Complete the if and elif statements!
return "A"
return "B"
return "C"
return "D"
else:
return “F”

# This should print an "A"

# This should print a "C"

# This should print an "F"

``````

I guess you missed the point of `fall through` raised earlier. The AND is moot.

Aside

Parens are not needed in Python if statements.

Thank you for the information, and I will correct my code.

3 posts were split to a new topic: The big if

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.