Python program to calculate the average score and final grade from 3 exams

Viewed 1687

I am making a python 3 program to calculate the average score and final grade (A, B, C, D, F) from 3 exams. Every time the "Your Grade" output is F. It Doesn't matter what your exam score is, the grade output is always F. What is wrong?

firstexam=0
secondexam=0
thirdexam=0
grade=0
finalgradeABC=0


firstexam = int(input('Enter your first exam: '))
secondexam = int(input('Enter your second exam: '))
thirdexam = int(input('Enter your third exam: '))


allthree =(firstexam + secondexam + thirdexam)


def average(allthree):
    return allthree/3


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


grades = allthree
finalgradeABC = yourgrade(grades)
avg = average(allthree)


print("Average grade is : " + str(avg))
print("You grade : " + str(finalgradeABC))
3 Answers

Your statement is incorrect. If you enter three scores of 33, you get an 'A'.

The functional problem is that you failed to take the average:

allthree =(firstexam + secondexam + thirdexam)

Try the correct equation:

allthree =(firstexam + secondexam + thirdexam) / 3

The structural problem is that you didn't properly account for all cases: you have gaps in your grading scale (i.e. 89.5 is an F), and your final clause assumes that anything other than those ranges must be a failing grade. Instead, try:

elif: avg < 60:
    return `F`
else:
    return "bad input or logic error"

I'm assuming you want the letter grade for the average grade. If that is the case, then you should call yourgrade() on avg after you calculate the average, like so:

grades = allthree
avg = average(allthree)
finalgradeABC = yourgrade(avg)

Change your last few lines to the following - you set up a nice average function, but you forgot to use it before passing a value to the yourgrade() function.

avg = average(allthree)
grades = avg
finalgradeABC = yourgrade(grades)
Related