David David - 3 months ago 8
Python Question

Find average of list + 2 inputs - python

I need to find the average of 5 assignments = two inputs. I'm getting a result, but its no correct. Here is what I have:

def main():

# list of grades
x = []

# count of students
n = 5

# fill list with grades from console input
# using pythonic generator
x = [int(input('Please enter the grade you got on assignment {}: '.format(i+1))) for i in range(n)]

midTermGrade = int(input('Please enter the grade you got on you Mid-Term: '))
finalGrade = int(input('Please enter the grade you got on you Final: '))

average_assignment_grade = sum(x) + midTermGrade + finalGrade / 7

print('Avg grade is ', average_assignment_grade)

main()


As you can see here, the math is wrong, the average suppose to be around 28, not 114

Python Console

Answer

There is a problem with order of operations. You need to change:

average_assignment_grade = sum(x) + midTermGrade + finalGrade / 7 

to:

average_assignment_grade = (sum(x) + midTermGrade + finalGrade) / 7 

You can read about Python operator precedence here.