I want to read in python from stdin and it looks like this:
(Group),(Grade):
1gT,8
1gT,5
1gT,9
1gT,8
1gX,4
1gX,4
1gX,7
1gZ,2
1gZ,9
1gZ,10
Now I want to calculate the averages per group. I know I can read from stdin with
for line in sys.stdin:
And I know how to calculate the averages:
([Sum of all grades from one group] / [number of grades of one group])
But how can I read the grades per group and count the number of it in Python3?
Thank you all for thinking. Thanks to Watanabe.N for helping, with a few edits on your answer it worked.
This worked for me:
import sys
ave = 0
total = 0
count = 0
firstline = sys.stdin.readline()
group, grade = firstline.split()
currentGroup = group
grade = int(grade)
total += grade
count += 1
for line in sys.stdin:
group, grade = line.split()
grade = int(grade)
if currentGroup != group:
print(currentGroup, ave)
count = 1
total = 0 + grade
currentGroup = group
continue
count += 1
total += grade
ave = total/count
else:
print(currentGroup, ave)