943,714 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 676
  • Python RSS
Oct 29th, 2008
0

Need a bit of Help

Expand Post »
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.
Last edited by BmeEng; Oct 29th, 2008 at 11:44 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BmeEng is offline Offline
8 posts
since Oct 2008
Oct 29th, 2008
0

Re: Need a bit of Help

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.
Last edited by sneekula; Oct 29th, 2008 at 11:21 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 29th, 2008
0

Re: Need a bit of Help

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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Oct 29th, 2008
0

Re: Need a bit of Help

Fixed. Sorry about that. Took me awhile to find the code tags button.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BmeEng is offline Offline
8 posts
since Oct 2008
Oct 29th, 2008
0

Re: Need a bit of Help

look here to see how someone else did it.

http://www.daniweb.com/forums/showth...=class+average
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 29th, 2008
0

Re: Need a bit of Help

Click to Expand / Collapse  Quote originally posted by tyincali ...
look here to see how someone else did it.

http://www.daniweb.com/forums/thread...=class+average
I get 404 File Not Found
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BmeEng is offline Offline
8 posts
since Oct 2008
Oct 29th, 2008
0

Re: Need a bit of Help

huh, just search for "class average" you'll find something to help you.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: Need a bit of Help

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
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007
Nov 4th, 2008
0

Re: Need a bit of Help

Thanks to all those who replied. I eventually figured it out though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BmeEng is offline Offline
8 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Memory game
Next Thread in Python Forum Timeline: Importing RSS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC