Test Average and Grade – Python

To have it print the letter grade as a user enters a score you can do something like:

#Calculate average
def calc_average(total):
    return total / 5

#Grading scale
def determine_score(grade):
    if 90 <= grade <= 100:
        return 'A'
    elif 80 <= grade <= 89:
        return 'B'
    elif 70 <= grade <= 79:
        return 'C'
    elif 60 <= grade <= 69:
        return 'D'
    else:
        return 'F'

#Enter 5 test scores
scores = []
for i in range(1, 6):
    score = int(input('Enter score {0}: '.format(i)))
    print 'That\'s a(n): ' + determine_score(score)
    scores.append(score)

total = sum(scores)
avg = calc_average(total)
abc_grade = determine_score(avg)


print('Average grade is: ' + str(avg))
print("That's a(n): " + str(abc_grade))

The letter grade problem was that you were using total which was all the grades added up (hopefully more than 100 if you got at least a 20 on all 5 assignments), so it would always default to the else. You want to use the avg so that it would give you the letter grade corresponding to the average:

abc_grade = determine_score(avg)

You can also omit the grade = total line because you will never use grade after making this change.

Leave a Comment

error code: 523