I have a question. I am trying to write a program that takes as input a list who
ID: 3547590 • Letter: I
Question
I have a question. I am trying to write a program that takes as input a list whose items are lists of three numbers. Each three-number list represents the three grades a particular student received for a course. For example, here is an input list for a class of four students: [[95,92,86], [66,75,54], [89,72,100], [34,0,0]]
this function should print, on the screen, two lines. The first line will contain a list containing every student's average grade. The second line will contain just one number: the average class grade, defined as the average of all students average grades. For instance here is the invoke and the output
>>> avgavg([[95,92,86], [66,75,54], [89,72,100], [34,0,0]])
[91.0,65.0,87.0,11.33333333333334]
63.58333333333
However, my first step is iterating over the lists. My logic is a big flawed. I am having trouble trying to get the function to iterate over each item of the list just once. Here is my code so far as I am breaking the problem down into subproblems-i am stariting with this problem which is iteration over the items:
def avgavg(lst):
studentAvg = []
#Avg = sum(lst)/len(lst)
for row in lst:
for item in row:
print(row)
and here is my output:
[95, 92, 86]
[95, 92, 86]
[95, 92, 86]
[66, 75, 54]
[66, 75, 54]
[66, 75, 54]
[89, 72, 100]
[89, 72, 100]
[89, 72, 100]
[34, 0, 0]
[34, 0, 0]
[34, 0, 0]
Does anyone have any hints as to what I am doing wrong?
Thanks
Explanation / Answer
For better readable code see here : http://pastebin.com/KsN2cJSa
def avgavg(lst):
studentAvg = []
for row in lst:
studentTotal=0
for item in row:
studentTotal=studentTotal+item #summing marks of a student
studentTotal=studentTotal/3 #Find student average
studentAvg.append(studentTotal) #add this avg to list of averages
print(studentAvg)
avgTotal=0
for item in studentAvg:
avgTotal=avgTotal+item #now add each avg to get overall average.
avgTotal=avgTotal/len(studentAvg) #divide by number of student
print (avgTotal) #dispaly overall average
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.