0

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()
1
  • 1
    Add parenthesis after f.close() Commented Feb 15, 2017 at 5:49

1 Answer 1

1

You should fill student names to names variable in main function.

In addition, i variable in init_Student function should be initialized to 0 because first index in array is 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)
Sign up to request clarification or add additional context in comments.

3 Comments

Man. python is too hard to understand :( Thanks for the help!!
Hi @David, I am glad to be of service to you. If this answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
0 does not appear to be a valid id for a Student; find_student reserves 0 as a "not found" signal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.