Python Python: Calculating the average of scores and separatign into letter grades

AI Thread Summary
The discussion revolves around a Python program that processes a file containing numerical scores, converting them into letter grades and calculating the average score. The user successfully opens the file and counts occurrences of each score but struggles with correctly summing the scores and calculating the average. Suggestions include using a loop to sum scores and employing a more efficient approach to categorize scores into letter grades without excessive memory usage. The conversation highlights the importance of clear logic in programming and offers a more Pythonic way to achieve the desired results. Ultimately, the user finds a solution and expresses gratitude for the assistance provided.
vipertongn
Messages
97
Reaction score
0

Homework Statement


I'm suppose to write up a program where I need to take a file that contains a list of
numbers. Example:
30
40
40
80

and so on, these number represent letter grades. 90~100 = „A‟, 80~89 = „B‟, 70~79 = „C‟, 60~69 = „D‟, all other scores = „F‟. So what I'm suppose to do for part of the homework is to separate the numbers into letter grades and then calculate the value of the average scores. The complete homework asks for a graph as well, but I'll figure that out as soon as I get this.

The Attempt at a Solution



Ok I got my program to open the file, by inputing the filename and etc. and its a dat file with a list of numbers. I do a loop for line in homework.readlines(): and converted the numbers (read as a string I believe) into integers using h = int(line) i think I'm running into a problem here because the numbers are not being calculated properly. Then I counted the number of each of the score using
scorecount = [0]*101
scorecount[h]=scorecount[h]+1

From here my question being, is it possible to do a sum calculation within the scorecount? like instead of doing scorecount[1]+scorecount[2]+scorecount[3], is it possible to find the sum by doing a range? like sum(scorecount[range(1-3)]) or somethign along those lines... I tried doing it the normal way but i end up with a sum of 0 for the counts of each number for the letter =/

That's one problem that I'm having, - For Average calculation I was able to get that you can divide by the sum of the total values found for A, B, C, D and F and on teh top you can get the sum of all of them by scorecount[line]*line for every number, (thing is I want to do that as well as add each and every number together so its like count[1]*1+count[2]*2...etc so say there was a count of 3 ones and 2 twos so it'd be like 3*1+2*2) I don't know how to write that out for python...my guess would be something using sum()

Example of the problem if there are two 90s, then scorecount[line]*line ~ scorecount[90] * 90 ~ 2 *90. What I want is for each of the line of numbers to be added to one another.
 
Last edited:
Technology news on Phys.org
vipertongn said:

Homework Statement


I'm suppose to write up a program where I need to take a file that contains a list of
numbers. Example:
30
40
40
80

and so on, these number represent letter grades. 90~100 = „A‟, 80~89 = „B‟, 70~79 = „C‟, 60~69 = „D‟, all other scores = „F‟. So what I'm suppose to do for part of the homework is to separate the numbers into letter grades and then calculate the value of the average scores. The complete homework asks for a graph as well, but I'll figure that out as soon as I get this.
Your explanation of what you need to do is not very clear. I would think that what you want to do is to find the average of the scores, and then convert that average to a letter grade. For the four numbers you give, the average is 190/4 = 47.5, which would be a grade of F.

What you said about "separat[ing] the numbers into letter grades and then calculat[ing] the value of the average scores" doesn't make any sense to me.

To calculate the sum of a bunch of numbers you should use a for loop. Then the average is the sum divided by how many numbers were added.

To convert to a letter grade, you can do it with an if ... else if ... else if ... else control structure or you can use a switch statement with a case for each letter grade.
vipertongn said:

The Attempt at a Solution



Ok I got my program to open the file, by inputing the filename and etc. and its a dat file with a list of numbers. I do a loop for line in homework.readlines(): and converted the numbers (read as a string I believe) into integers using h = int(line). Then I counted the number of each of the score using
scorecount = [0]*101
scorecount[h]=scorecount[h]+1

From here my question being, is it possible to do a sum calculation within the scorecount? like instead of doing scorecount[1]+scorecount[2]+scorecount[3], is it possible to find the sum by doing a range? like sum(scorecount[range(1-3)]) or somethign along those lines...

That's one problem that I'm having, the second problem would be - For Average calculation I was able to get that you can divide by the sum of the total values found for A, B, C, D and F and on teh top you can get the sum of all of them by scorecount[line]*line for every number, (thing is I want to do that as well as add each and every number together so its like count[1]*1+count[2]*2...etc so say there was a count of 3 ones and 2 twos so it'd be like 3*1+2*2) I don't know how to write that out for python...my guess would be something using sum()

Example of the problem if there are two 90s, then scorecount[line]*line ~ scorecount[90] * 90 ~ 2 *90. What I want is for each of the line of numbers to be added to one another.
 
oh I'm sorry for the unclear explanation, I expected it to be so. Here's what I'm trying to say. I have a list of numbers, its a lot of numbers within a .dat file that I'll have python open using homework = open(File, 'r')
90
80
66
50
and so forth, like the 90 would be considered an A, teh 80 would be considered a B and 66 would be a D and 50 is an F. so what I'm doing is first counting the number of each of these scores and then sorting them into each letter category. (this is so then I can get a histogram graph if letter grades which I'm not focusing on right now).

Besides sorting out the letter grades I need to find the average of all the scores, which is easy, the sum of all of the line scores dividing with all the counts...but writing it out is confusing

Ok here's what my program looks like so far...:

File = raw_input("Please enter file:")
infile = open(File, 'r')
for line in infile.readlines():
score = int(line)
counts = [0] * 101
counts[score] = counts[score] + 1
A = counts[100]+counts[99]+counts[98]+counts[97]+counts[96]+counts[95]+counts[94]+counts[93]+counts[92]+counts[91]+counts[90]
B = counts[89]+counts[88]+counts[87]+counts[86]+counts[85]+counts[84]+counts[83]+counts[82]+counts[81]+counts[80]
C = counts[79]+counts[78]+counts[77]+counts[76]+counts[75]+counts[74]+counts[73]+counts[72]+counts[71]+counts[70]
D = counts[69]+counts[68]+counts[67]+counts[66]+counts[65]+counts[64]+counts[63]+counts[62]+counts[61]+counts[60]

infile.close()
 
No offense meant, but your code for separating out the letter grades is pretty ugly. I know almost nothing about python, so I'll show you some C/C++ code to separate out the letter grades.

Here score is the current test score. A_count, B_count, etc. are the counts of the letter grades.
Code:
int A_count = 0, B_count = 0, C_count = 0, D_count = 0, F_count = 0;
.
.
.
// Code for opening file and reading test scores not shown.

if ((score >= 90) && (score <= 100)) A_count++;
else if ((score >= 80) && (score < 90)) B_count++;
else if ((score >= 70) && (score < 80)) C_count++;
else if ((score >= 60) && (score < 70)) D_count++;
else F_count++;
 
i know wat u mean -_-;; this is a homework assignment that confuses even myself... but eh...
 
This program doesn't need any arrays. You can do the letter counts and the sum of all numbers (as well as the count of the number of grades) within the loop that extracts the numbers. There is no reason for excess memory usage.

Some pseudocode:

Code:
int A_count = 0, B_count = 0, ... F_count = 0;
double sum = 0;
int numberTotal = 0;
double number;

while there are more numbers
     number = a number from the data file
     if(number is an A) ++A_count
     else if(number is a B) ++B_count
     ...
     else ++F_count
     sum += number;
     ++numberTotal;
end while

//calculate average and finish program...

In case you're not familiar with the syntax:
++variable means variable = variable + 1 (increment by 1)
variable += variable2 means variable = variable + variable2 (increment variable by variable2)
 
There are a millions way to fiddle with the logic of this problem, so I will just edit what you have here to make it more python-like. The end result will still be an array of length 101, where counts[x] will be the number of times score x appeared, as well as the other desired quantities.

Code:
File = raw_input("Please enter file:")
infile = open(File, 'r')
counts = [0] * 101 #don't construct this inside your loop, as before

for line in infile.readlines():
        score = int(line)
        counts[score] = counts[score] + 1 
#can't remember if counts[score] += 1 works or not - it does for some packages at least
infile.close()

total = 0
for i,c in enumerate(counts):
        total = total + i*c
average = float(total)/float(sum(counts))

A = sum(counts[90:])
B = sum(counts[80:90])
C = sum(counts[70:80])
D = sum(counts[60:70])
F = sum(counts[0:60])
That should do it. I would use different logic if I were writing this, but there's never a "right" answer for programming. Hope this helps.
 
Oh hey thanks, I was able to find out my own way of doing it, but the float part helped me a lot. Someone mind deleting this thread now? thanks.
 
Threads when finished don't get deleted.
 

Similar threads

Replies
4
Views
4K
Replies
2
Views
839
Replies
8
Views
1K
Replies
10
Views
2K
Replies
9
Views
3K
Replies
12
Views
8K
Replies
5
Views
2K
Back
Top