Is there a problem with writing codes or writing codes on Python?

Viewed 58

I want to calculate a class' notes from Folder.txt with note_calculate. And I can calculate the grades as well. After calculating, I insert in list(Which called allnames). After that I took the information from that list to a new txt folder called Namesandnotes.txt. The problem starts now. I want to write Passed Students and Failed Students in a .txt folder. "CC" and higher grades should write in Passed Students.txt, lower grades should write in Failed Students.txt. But in the code, I can't write names and grades from Namesandgrades.txt to Passed and Failed. That is my problem.

def note_calculate(line):
    line = line[:-1]
    list1 = line.split(",")
    name = list1[0]
    note1 = int(list1[1])
    note2 = int(list1[2])
    final = int(list1[3])
    son = note1 * (3/10) + note2 * (3/10) + final * (4/10)
    passed = []
    failed = []

    if (son >= 90):
        grade = "AA"
    elif (son >= 85):
        grade = "BA"
    elif (son >= 80):

        grade = "BB"
    elif (son >= 75):

        grade = "CB"
    elif (son >= 70):

        grade = "CC"
    elif (son >= 65):

        grade = "DC"
    elif (son >= 60):

        grade = "DD"
    elif (son >= 55):

        grade = "FD"
    else:
        grade = "FF"
    return name +","+ grade



with open("Folder.txt","r", encoding="utf-8") as file:
    allnames = list()
    for i in file:
        allnames.append(note_calculate(i))

    with open("Namesandnotes.txt","r+", encoding = "utf-8") as file2:

        for a in allnames:
            file2.write(a + "\n")

with open("Namesandnotes.txt","r", encoding="utf-8") as file3:
    passed = list()
    failed = list()
    text = file3.read()
    print(text)
    for line in file3:
        line = line[:-1]
        line_element = line.split(",")

        if (line_element[1] == "AA"):
            passed.append(line_element[1] + "\n")
        elif (line_element[1] == "BA"):
            passed.append(line_element[1] + "\n")
        elif line_element[1] == "BB":
            passed.append(line_element[1] + "\n")
        elif line_element[1] == "CB":
            passed.append(line_element[1] + "\n")
        elif line_element[1] == "CC":
            passed.append(line_element[1] + "\n")
        else:
            failed.append(line_element[1] + "\n")


    with open("PassedStudents.txt","r+",encoding="utf-8") as file4:
        for x in passed:
            file4.write(x + "\n")
    with open("FailedStudents.txt", "r+", encoding="utf-8") as file5:
        for c in failed:
            file5.write(c + "\n")
1 Answers

The problem is text = file3.read(). Once you've done this, the cursor is at the end of the file, so later when you iterate through file3 line by line, there is nothing left to read at the end of the file, and nothing gets written either. Just delete that one line and it should work.

For further debugging, I also recommend putting in prints at other places, so you can see which conditions and loops your code actually enters.

Related