我想用Folder.txt
从note_calculate
计算一个班级的笔记。我也可以计算成绩。计算之后,我插入列表(称为allnames
)。之后,我将信息从该列表中移到名为Namesandnotes.txt
的新txt文件夹中。问题现在开始。我想在Passed Students
文件夹中写入Failed Students
和.txt
。 “ CC”和更高的分数应该用Passed Students.txt
书写,较低的分数应该用Failed Students.txt
书写。但是在代码中,我无法写出从Namesandgrades.txt
到通过和失败的名称和等级。那是我的问题。
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")
答案 0 :(得分:4)
问题是text = file3.read()
。完成此操作后,游标将位于文件的末尾,因此稍后在逐行遍历file3时,在文件末尾将没有任何内容可读取,也不会写入任何内容。只需删除那一行,它应该可以工作。
为了进行进一步的调试,我还建议在其他地方放置打印件,以便您可以查看代码实际输入的条件和循环。