The big if (I'm stuck on that)


#1

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.

Your function should return the following letter grades:

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:

if grade >= 90:
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.


#2

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


#3

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.


#4

I FINALLY GOT THE ANSWER!<o/

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!

Complete the if and elif statements!

def grade_converter(grade):
if grade >=90:
return "A"
elif (grade <=89) and (grade >=80):
return "B"
elif (grade <=79) and (grade>=70):
return "C"
elif (grade >=65) and (grade <=69):
return "D"
else:
return “F”

This should print an “A”

print grade_converter(92)

This should print a “C”

print grade_converter(70)

This should print an “F”

print grade_converter(61)

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/


#5
def get_letter_grade(grade):
    if grade >= 90:
        return "A"
    elif grade >= 80:
        return "B"
    elif grade >= 70:
        return "C"
    elif grade >= 60:
        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.


#6

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!
def grade_converter(grade):
    if grade >= 90:
        return "A"
    elif grade >= 80:
        return "B"
    elif grade >= 70:
        return "C"
    elif grade >= 60:
        return "D"
    else:
        return “F”
      
# This should print an "A"      
print grade_converter(92)

# This should print a "C"
print grade_converter(70)

# This should print an "F"
print grade_converter(61)


#7

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

Aside

Parens are not needed in Python if statements.


#8

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


#9

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


#10

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