repl.it
@TreyvanMcDonald/

Coding with Text Files - 2nd Assignment

Python

No description

fork
loading
Files
  • main.py
  • 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
46
47
48
49
50
51
52
53
54
55
56
print()
print("Date: 2/25/2019")
print("Name: Treyvan McDonald")
print("Period 8: 8th Period Coding")
print("Creating a bigger Text File and then reading it.")
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
totalGrade = 0

for x in range(1, 4):
  #print("Line", cnt, ": " + line)
  splitLineUp = line.split(",")
  
  #print(split_line_up)
  name = splitLineUp[0]
  print(name)
  
  subject = splitLineUp[1]
  
  if cnt == 1:
    mathGrades = [int(splitLineUp[2]), int(splitLineUp[3]), int(splitLineUp[4])]
    totalGrade+=mathGrades[0]
    totalGrade+=mathGrades[1]
    totalGrade+=mathGrades[2]
    print("Subject: "+subject, "Grades: ", mathGrades, "Average: ", totalGrade/3)
    totalGrade = 0
  if cnt == 2:
    scienceGrades = [int(splitLineUp[2]), int(splitLineUp[3]), int(splitLineUp[4]), int(splitLineUp[5])]
    totalGrade+=scienceGrades[0]
    totalGrade+=scienceGrades[1]
    totalGrade+=scienceGrades[2]
    totalGrade+=scienceGrades[3]
    print("Subject: "+subject, "Grades: ", scienceGrades,"Average: ", totalGrade/4)
    totalGrade = 0
  if cnt == 3:
    ELARGrades = [int(splitLineUp[2]), int(splitLineUp[3]), int(splitLineUp[4])]
    totalGrade+=ELARGrades[0]
    totalGrade+=ELARGrades[1]
    totalGrade+=ELARGrades[2]
    print("Subject: "+subject, "Grades: ", ELARGrades, "Average: ", totalGrade/3)
    totalGrade = 0

  print()
  line = fp.readline()
  cnt+=1

fp.close()