[英]No Error but Empty output file.
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和quiz.txt),其中包含学生的姓名以及他们的硬件和测验分数,例如:
hw.txt hw.txt
John 100 约翰100
David 100 大卫100
John 50 约翰50
John 75 约翰75
Ellen 12 艾伦12
David 23 大卫23
Helen 60 海伦60
quiz.txt quiz.txt
John 50 约翰50
David 70 大卫70
john 25 约翰25
Ellen 100 艾伦100
Helen 100 海伦100
and after reading two files, the below program should combine the data into one single text file(scores.txt). 在读取两个文件之后,下面的程序应将数据合并为一个文本文件(scores.txt)。 The program runs without any errors but the output text file(scores.txt) contains nothing oO?
程序运行无任何错误,但输出文本文件(scores.txt)不包含任何内容。 Been digging stack-overflow Reddit everywhere but found no solution.
到处都在挖掘堆栈溢出Reddit,但没有找到解决方案。
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()
You should fill student names to names
variable in main
function. 您应该在
main
功能的names
变量中填写学生姓名。
In addition, i
variable in init_Student
function should be initialized to 0 because first index in array is 0. 另外,由于数组中的第一个索引为0,因此
init_Student
函数中的i
变量应初始化为0。
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 = 0 # Initialize to 0 for index access
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 = ['John', 'David'] # Fill student names
student_list = []
init_Student(names, student_list)
get_HW_Scores("hw.txt", student_list)
get_Quiz_Scores("quiz.txt", student_list)
print(student_list)
output_Scores(student_list)
main()
The result. 结果。
$ cat scores.txt
John
HW_Percent: 0%
Quiz_Percent: 0%
Overall: 0.0%(F)
David
HW_Percent: 64%
Quiz_Percent: 64%
Overall: 64.0%(D)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.