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

  • Context: Python 
  • Thread starter Thread starter vipertongn
  • Start date Start date
  • Tags Tags
    Average Grades Python
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment involving the creation of a Python program to read a file containing numerical scores, categorize these scores into letter grades, and calculate the average score. Participants are exploring various approaches to implement the program, including handling file input, counting scores, and calculating averages.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to read scores from a file and count occurrences of each score using an array, questioning how to sum values over a range of indices.
  • Another participant suggests that the explanation of the task is unclear and proposes that the average should be calculated directly from the scores rather than from letter grade counts.
  • A participant provides a code snippet in C/C++ to illustrate how to separate letter grades, indicating a preference for a more straightforward approach than what was presented in Python.
  • Some participants express confusion over the assignment and the complexity of the task, with one acknowledging the difficulty of the homework.
  • Another participant suggests that the program could be simplified by avoiding arrays and instead calculating counts and sums directly within the loop that processes the scores.

Areas of Agreement / Disagreement

There is no consensus on the best approach to implement the program. Participants present different methods and opinions on how to handle the calculations and data structures, indicating multiple competing views on the solution.

Contextual Notes

Participants mention various programming constructs and methods, such as loops and conditionals, but there is uncertainty regarding the most efficient or clear way to implement the required functionality in Python. Some participants also reference pseudocode, but no definitive solution is agreed upon.

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 the 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 the 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, the 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 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 12 ·
Replies
12
Views
9K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K