I have following program which reads two text files(hw.txt and quiz.txt) which contains name of students and scores of their hw and quiz such as:
hw.txt
John 100
David 100
John 50
John 75
Ellen 12
David 23
Helen 60
quiz.txt
John 50
David 70
john 25
Ellen 100
Helen 100
and after reading two files, the below program should combine the data into one single text file(scores.txt). The program runs without any errors but the output text file(scores.txt) contains nothing o.O? Been digging stack-overflow Reddit everywhere but found no solution.
class Student(object):
def __init__(self,id, name):
self.id = id
self.name = name
self.hw = 0
self.quiz = 0
def init_Student(names,student_list):
i = 1
for j in names:
student_list.append(Student(i,j))
i += 1
def find_student(student_list,name):
for x in student_list:
if x.name == name:
return x.id
return 0
def get_HW_Scores(file_name, student_list):
with open(file_name, 'r') as f:
for line in f:
i = find_student(student_list, line.split(" ")[0])
if i != 0:
student_list[i].hw += int(line.split(" ")[1])
def get_Quiz_Scores(file_name, student_list):
with open(file_name, 'r') as f:
for line in f:
i = find_student(student_list, line.split(" ")[0])
if i != 0:
student_list[i].quiz += int(line.split(" ")[1])
def assign_grade(score):
if score >=97:
return "A+"
elif score >=93:
return "A"
elif score >=90:
return "A-"
elif score >=87:
return "B+"
elif score >=83:
return "B"
elif score >=80:
return "B-"
elif score >=77:
return "C+"
elif score >=73:
return "C"
elif score >=70:
return "C-"
elif score >=67:
return "D+"
elif score >=63:
return "D"
elif score >=60:
return "D-"
elif score <60:
return "F"
def output_Scores(student_list):
f = open("scores.txt", 'w')
for x in student_list:
f.write(x.name + "\n")
f.write("HW_Percent: " + str(x.hw/3) + "% \n")
f.write("Quiz_Percent: " + str(x.quiz/3) + "% \n")
num = (x.hw/3)*0.5 + (x.quiz/3)*0.5
f.write("Overall: " + str(num) + "%" "(" + assign_grade(num) + ")" + "\n")
f.close
def main():
names = []
student_list = []
init_Student(names, student_list)
get_HW_Scores("hw.txt", student_list)
get_Quiz_Scores("quiz.txt", student_list)
output_Scores(student_list)
main()
f.close()