Hi folks,
I'm learning python this year, and I need a bit of help on an assignment.
My program is supposed to be able to read from a file, which contains student numbers and their scores and it is supposed to compute their individual averages and the class average and then assign a grade based upon that.

I'm stuck on successfully calculating the true class average and comparing that value to the individual averages of each student. Specifically, the sum of the student averages seems to be the problem.

Here is what I have so far and the output. The output shows the main problem.

sum = 0
count = 0
avg_sum = 0
file = open("scores.txt","r")
lines = file.readlines()
line_count = len(lines)
file.close()
for line in lines:
    scores = line.split()
    count = 0
    sum = 0.0
    for score in scores:
        if count != 0:
            x = int(score)
            sum = sum + x
            count = count + 1
        else:
            count = count + 1
    count = count - 1
    if count !=0:
        avg = int(sum/count)
    else:
        avg = 0
    avg_sum = avg_sum + avg
    class_avg = int(avg_sum/line_count)
    if(avg >= class_avg):
        print "SID:", scores[0], "Average:", avg, "Grade: P", "class average sum:", avg_sum, "class average:", class_avg
    else:
        print "SID:", scores[0], "Average:", avg, "Grade: NP", "class average sum:", avg_sum, "class average:", class_avg
print "Class Average:", class_avg

SID: 1001 Average: 0 Grade: P class average sum: 0 class average: 0
SID: 1002 Average: 90 Grade: P class average sum: 90 class average: 11
SID: 1003 Average: 100 Grade: P class average sum: 190 class average: 23
SID: 1004 Average: 20 Grade: NP class average sum: 210 class average: 26
SID: 1005 Average: 80 Grade: P class average sum: 290 class average: 36
SID: 1006 Average: 70 Grade: P class average sum: 360 class average: 45
SID: 1007 Average: 13 Grade: NP class average sum: 373 class average: 46
SID: 1008 Average: 70 Grade: P class average sum: 443 class average: 55
Class Average: 55

Any guidance would be appreciated.

This old horse of homework has come up a fair number of times here. Maybe you ought to search for it. Don't forget to use code tags so we can actually read what you are doing so far.

Can you please put your code in python CODE tags so it has syntax highlighting, but most importantly indentation. Seeing as Python requires indentation, and the above has none, it's hard to sort through at the moment.

Fixed. Sorry about that. Took me awhile to find the code tags button.

huh, just search for "class average" you'll find something to help you.

Thanks to all those who replied. I eventually figured it out though! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.