repl.it
@TreyvanMcDonald/

Coding with Text Files - 3rd Assignment

Python

No description

fork
loading
Files
  • main.py
  • averages.txt
  • grades.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
print()
print("Date: 2/25/2019")
print("Name: Treyvan McDonald")
print("Period 8: 8th Period Coding")
print("Coding with Text Files - 3rd Assignment")
print()

fh = open("grades.txt", "w")
fh.write("John Smith, Math, 68, 93, 72, \n")
fh.write("John Smith, Science, 98, 91, 88, 94, \n")
fh.write("John Smith, ELAR, 82, 88, 91, \n")
fh.close()

fp = open("grades.txt", "r")
line = fp.readline()
cnt = 1

while line:
  splitLineUp = line.split(",")
  
  name = splitLineUp[0]

  if cnt == 1:
    subject = splitLineUp[1]
    mathGrades = int(splitLineUp[2]) + int(splitLineUp[3]) + int(splitLineUp[4])
    mathAverage = mathGrades / 3
  if cnt == 2:
    subject2 = splitLineUp[1]
    scienceGrades = int(splitLineUp[2]) + int(splitLineUp[3]) + int(splitLineUp[4]) + int(splitLineUp[5])
    scienceAverage = scienceGrades / 4
  if cnt == 3:
    subject3 = splitLineUp[1]
    ELARGrades = int(splitLineUp[2]) + int(splitLineUp[3]) + int(splitLineUp[4])
    ELARAverage = ELARGrades / 3
  
  line = fp.readline()
  cnt += 1

fp.close()

fh = open("averages.txt", "w")
fh.write(name+"\t"+subject+"\t\t"+str(mathAverage)+"\n")
fh.write(name+"\t"+subject2+"\t\t"+str(scienceAverage)+"\n")
fh.write(name+"\t"+subject3+"\t\t"+str(ELARAverage)+"\n")
fh.close()