1

This is my data file (called “studentdata.txt”)

joe 10 15 20 30 40
bill 23 16 19 22
sue 8 22 17 14 32 17 24 21 2 9 11 17
grace 12 28 21 45 26 10
john 14 32 25 16 89

I need to calculate the average grade for each student and print out the student’s name along with their average grade. I can extract the name with no problem and determine the number of exam scores, but I can not figure out how to sum the exam scores. This is what I have so far:

file=open("studentdata.txt","r")

for aline in file:    
    data=aline.split()    

    print((data[0]),"Average grade:")
    print(len(data[1:]))


file.close()  
Ranger
  • 43
  • 5
  • Seems to me like you would need to loop through `data[1] -> data[len(data)-1]` and add up each test score, to get the average you would then need to divide by the total number of tests for each user. Pretty trivial. – Matt Clark Jun 07 '18 at 23:19
  • I understand how to average the scores, my problem is I can't seem to get just the numbers to convert to integers so I can sum them since each student has a different number of tests....I guess my basic question is how do I convert the numbers to integers for each student? – Ranger Jun 07 '18 at 23:25
  • 1
    Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – CDspace Jun 07 '18 at 23:53

3 Answers3

3

It seems like you have most of this already done, and you already have a good grasp of how to partition each line into the two components you need, so you're real close!

First, since the data is being read in as a string, you need to convert part of your data to integers:

for line in file:
    tmp = line.split()
    name, scores = tmp[0], list(map(int, tmp[1:]))

This will give us each name, along with a list of scores as integers. Now all you have to do is find the average:

average = sum(scores)/len(scores)

Let's tie it all together by assigning to a dictionary:

dct[name] = average

And we get:

{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • Excellent!!....Thanks so much, I couldn't figure out how to get the numbers in the string to convert to integers...Thanks again! – Ranger Jun 07 '18 at 23:35
  • Happy to help! Another option would be to use `[int(i) for i in tmp[1:]]` I would recommend using whichever is clearer to you – user3483203 Jun 07 '18 at 23:36
1

Try this?

file = open("studentdata.txt", "r")
for aline in file:
    data = aline.split()

    # Convert list of string numerals to int
    grades = [int(grade) for grade in data[1:]]

    # Find average by dividing sum by length of numbers list
    average = sum(grades)/len(data[1:])

    print((data[0]), "Average grade:", str(average))

file.close()
0

Try the below code, just split each line on spaces then get the numbers not with the name so the indexing will be the i.strip().split()[1:] then use map to convert that into an integer then use statistics.mean to get the average:

from statistics import mean
d = {}
with open('studentdata.txt','r') as f:
   for i in f.readlines():
      d[i.split()[0]] = mean(list(map(int,i.strip().split()[1:])))
print(d)

Output:

{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}
U13-Forward
  • 69,221
  • 14
  • 89
  • 114