954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need a bit of Help

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.

BmeEng
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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.

shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77
 

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

BmeEng
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
tyincali
Light Poster
45 posts since Oct 2008
Reputation Points: 31
Solved Threads: 7
 

look here to see how someone else did it.

http://www.daniweb.com/forums/thread66974.html&highlight=class+average


I get 404 File Not Found

BmeEng
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

tyincali
Light Poster
45 posts since Oct 2008
Reputation Points: 31
Solved Threads: 7
 

The problem with the link is that it highlits the words class and average and for some reason that doesn't work but if you just remove that part you do get to the right page.
http://www.daniweb.com/forums/thread66974.html

mathijs
Light Poster
41 posts since Oct 2007
Reputation Points: 10
Solved Threads: 2
 

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

BmeEng
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You