- #1
vipertongn
- 98
- 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: