Python上写代码或写代码有问题吗?
Is there a problem with writing codes or writing codes on Python?
我想用 note_calculate
从 Folder.txt
计算 class' 个音符。我也可以计算成绩。计算后,我插入列表(称为allnames
)。之后,我将该列表中的信息保存到一个名为 Namesandnotes.txt
的新 txt 文件夹中。问题现在开始了。我想在 .txt
文件夹中写入 Passed Students
和 Failed Students
。 "CC"高年级写Passed Students.txt
,低年级写Failed Students.txt
。但是在代码中,我不能写从 Namesandgrades.txt
到 Passed 和 Failed 的姓名和成绩。那是我的问题。
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")
问题是text = file3.read()
。完成此操作后,光标位于文件末尾,因此稍后当您逐行遍历 file3 时,文件末尾没有任何内容可读,也没有任何内容可写。只需删除那一行,它就可以工作了。
为了进一步调试,我还建议在其他地方打印出来,这样你就可以看到你的代码实际进入了哪些条件和循环。
我想用 note_calculate
从 Folder.txt
计算 class' 个音符。我也可以计算成绩。计算后,我插入列表(称为allnames
)。之后,我将该列表中的信息保存到一个名为 Namesandnotes.txt
的新 txt 文件夹中。问题现在开始了。我想在 .txt
文件夹中写入 Passed Students
和 Failed Students
。 "CC"高年级写Passed Students.txt
,低年级写Failed Students.txt
。但是在代码中,我不能写从 Namesandgrades.txt
到 Passed 和 Failed 的姓名和成绩。那是我的问题。
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")
问题是text = file3.read()
。完成此操作后,光标位于文件末尾,因此稍后当您逐行遍历 file3 时,文件末尾没有任何内容可读,也没有任何内容可写。只需删除那一行,它就可以工作了。
为了进一步调试,我还建议在其他地方打印出来,这样你就可以看到你的代码实际进入了哪些条件和循环。