Reputation: 29
The function avgavg()
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 (you never know how many students you will have to process):
[[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]
The function avgavg()
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 student average grades.
Output given the above data:
[91.0, 65.0, 87.0, 11.333333333334]
63.5833333333
(This is my assignment question. I have tried to start this a few different ways and I cannot seem to get my inputs to connect to anything. How would I go about solving this? This is my latest attempt: COLS = int(input("Enter the number of students: ")) ROWS = int(input("Enter the number of grades per student: "))
def avgavg():
student_grades = []
for c in range(COLS):
student = input("Enter the number of the student: ")
grades = []
for r in range (ROWS):
grade = eval(input("Enter the grades for the student: "))
grades.append(grade)
student_grades[student] = grades
print(grades)
Upvotes: 0
Views: 3108
Reputation: 308
This is to elaborate on sshashank124's answer and add the user input function. I have also added some error handling code (while loops), so if a user inputs a string instead of a grade, it will ask again:
Remember, don't use this exact code for your submission. This is probably an integrity violation in your class. Try to rewrite the code from scratch and tell what didn't go well
What you did wrong in your code, is that you've put the two for loops by themselves. They need to be nested.
error = "Non integer input. Try again>> " #Error message
def isfloat(value): #Functon to check if a number can be a grade
try:
float(value)
return True
except ValueError:
return False
n = raw_input("Enter number of students>> ") #Number of students
while not n.isdigit():
n = raw_input(error)
g = raw_input("\nEnter number of grades per student>> ") #Number of Grades per student
while not g.isdigit():
g = raw_input(error)
grades = [] #The two dimensional list
print
#Receiving input from the user
for a in range(int(n)):
temp = []
print "Student %i" % (a+1)
for b in range(int(g)):
grade = raw_input("Grade %i>> " % (b+1))
while not isfloat(grade):
grade = raw_input(error)
temp.append(float(grade))
grades.append(temp)
print
students = [sum(stud)/float(len(stud)) for stud in grades]
#Returning output
print students
print sum(students)/float(len(students))
Upvotes: 1
Reputation: 19264
Here is working code:
lists = eval(raw_input('Enter the class data as a list: '))
def avgavg(lists):
avg = [float(sum(sub))/float(len(sub)) for sub in lists]
everything = [item for sub in lists for item in sub]
allavg = float(sum(everything))/float(len(everything))
return avg, allavg
avg, allavg = avgavg(lists)
print avg
print allavg
This runs as:
bash-3.2$ python lists.py
Enter the class data as a list: [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]
[91.0, 65.0, 87.0, 11.333333333333334]
63.5833333333
bash-3.2$
This code takes input using eval(raw_input(...))
to strip the quotes from the string. We then use list comprehension to get the average of each one, and then uses list comprehension to make all the lists in the list of lists one list (sorry for all the lists :)). We then return it, and at the end print it out.
Don't hesitate to ask any questions below!
Hope this helps.
Upvotes: 0
Reputation: 13261
I realize this is an assignment, in the real world I wouldn't hesitate to use numpy:
> grades = [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]
> np.average(grades, axis=1)
array([ 91. , 65. , 87. , 11.33333333])
> np.average(grades)
63.583333333333336
Upvotes: 0
Reputation: 32189
To get the average of a list, say [1, 2, 3]
, you would do it as:
a = [1, 2, 3]
print sum(a)/float(len(a))
Hopefully that is enough to get you started. I will let you figure out the specifics. Also, I will only post the full solution once you have demonstrated sufficiently that you have tried working this out yourself and were unable to get it to work as expected.
Now that you have showed some effort:
a = [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]
b = [sum(i)/float(len(i)) for i in a]
>>> print b
[91.0, 65.0, 87.0, 11.333333333333334]
print sum(b)/float(len(b))
63.5833333333
Upvotes: 1