没有错误但是空输出文件。

时间:2017-02-15 05:47:52

标签: python

我有以下程序,它读取两个文本文件(hw.txt和quiz.txt),其中包含学生姓名和他们的hw和测验分数,如:

hw.txt

John 100

David 100

John 50

John 75

Ellen 12

David 23

Helen 60

quiz.txt

John 50

David 70

约翰25

Ellen 100

Helen 100

并且在读取两个文件之后,下面的程序应该将数据组合成一个单独的文本文件(scores.txt)。该程序运行没有任何错误,但输出文本文件(scores.txt)不包含o.O?一直在挖堆栈溢出的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()

1 个答案:

答案 0 :(得分:1)

您应该将学生姓名填入names函数中的main变量。

此外,i函数中的init_Student变量应初始化为0,因为数组中的第一个索引为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()

结果。

$ 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)