我的代码不会将学生成绩附加到文本文件中
My code won't append student grades into a text file
我正在尝试编写一个程序,允许教师输入学生的姓名和他们收到的成绩,然后将其输出到文本文件。这样做 12 次之后,代码应该会要求您提供具有成绩的文件的名称,称为“grades.txt”,在您输入 grades.txt 之后,它应该会读出您输入的学生的成绩和姓名。
count = 0
with open('grades.txt', 'w') as grades:
grades.write('Student Names and Grades:')
grades.write('\n')
while count <12:
count+=1
name=input(' Enter student name: ')
try:
average=int(input('Enter grade average: '))
if 0<= average <=100:
continue
else:
print('Average must be between 0 and 100')
except ValueError:
print ('Input grade must be a number')
with open('grades.txt', 'a') as grades:
grades.write('Student name: ')
grades.write(name)
grades.write(' -- Student average grade: ')
grades.write(str(average))
grades.write('\n')
grades.close()
try:
question=input('Enter file name for grades: ')
if question == 'grades.txt':
with open('grades.txt', 'r') as grades:
grades_read = grades.read()
print(grades_read)
grades.close()
except:
print('File not found')
但是,当文件被读出时,它只显示“学生姓名和成绩:”以及附加到它的姓名和成绩的 none。有谁知道为什么?
这是因为线
if 0<= average <=100:
continue
continue 会让您跳过其余代码并转到循环的下一次迭代(因此此处不在文件中打印)。此处针对您的应用程序的正确命令是“通过”而不是“继续”,只需替换并尝试一下。
顺便说一句:即使你把计数 < 12,它也只会计数 11(也许试试 <=)
你的continue
打错了,打对了就跳过
此外,当您使用上下文管理器(with
语句)时,您不需要关闭文件。使用 except FileNotFoundError
比使用 except
更好。
并且您可能只想在保存有效记录后才增加 count
,因此将其移至循环末尾。
count = 0
with open('grades.txt', 'w') as grades:
grades.write('Student Names and Grades:')
grades.write('\n')
while count < 12:
name = input(' Enter student name: ')
try:
average = int(input('Enter grade average: '))
if not 0 <= average <= 100:
print('Average must be between 0 and 100')
continue
except ValueError:
print('Input grade must be a number')
continue
with open('grades.txt', 'a') as grades:
grades.write('Student name: ')
grades.write(name)
grades.write(' -- Student average grade: ')
grades.write(str(average))
grades.write('\n')
count += 1
try:
question = input('Enter file name for grades: ')
if question == 'grades.txt':
with open('grades.txt', 'r') as grades:
grades_read = grades.read()
print(grades_read)
except FileNotFoundError:
print('File not found')
我正在尝试编写一个程序,允许教师输入学生的姓名和他们收到的成绩,然后将其输出到文本文件。这样做 12 次之后,代码应该会要求您提供具有成绩的文件的名称,称为“grades.txt”,在您输入 grades.txt 之后,它应该会读出您输入的学生的成绩和姓名。
count = 0
with open('grades.txt', 'w') as grades:
grades.write('Student Names and Grades:')
grades.write('\n')
while count <12:
count+=1
name=input(' Enter student name: ')
try:
average=int(input('Enter grade average: '))
if 0<= average <=100:
continue
else:
print('Average must be between 0 and 100')
except ValueError:
print ('Input grade must be a number')
with open('grades.txt', 'a') as grades:
grades.write('Student name: ')
grades.write(name)
grades.write(' -- Student average grade: ')
grades.write(str(average))
grades.write('\n')
grades.close()
try:
question=input('Enter file name for grades: ')
if question == 'grades.txt':
with open('grades.txt', 'r') as grades:
grades_read = grades.read()
print(grades_read)
grades.close()
except:
print('File not found')
但是,当文件被读出时,它只显示“学生姓名和成绩:”以及附加到它的姓名和成绩的 none。有谁知道为什么?
这是因为线
if 0<= average <=100:
continue
continue 会让您跳过其余代码并转到循环的下一次迭代(因此此处不在文件中打印)。此处针对您的应用程序的正确命令是“通过”而不是“继续”,只需替换并尝试一下。
顺便说一句:即使你把计数 < 12,它也只会计数 11(也许试试 <=)
你的continue
打错了,打对了就跳过
此外,当您使用上下文管理器(with
语句)时,您不需要关闭文件。使用 except FileNotFoundError
比使用 except
更好。
并且您可能只想在保存有效记录后才增加 count
,因此将其移至循环末尾。
count = 0
with open('grades.txt', 'w') as grades:
grades.write('Student Names and Grades:')
grades.write('\n')
while count < 12:
name = input(' Enter student name: ')
try:
average = int(input('Enter grade average: '))
if not 0 <= average <= 100:
print('Average must be between 0 and 100')
continue
except ValueError:
print('Input grade must be a number')
continue
with open('grades.txt', 'a') as grades:
grades.write('Student name: ')
grades.write(name)
grades.write(' -- Student average grade: ')
grades.write(str(average))
grades.write('\n')
count += 1
try:
question = input('Enter file name for grades: ')
if question == 'grades.txt':
with open('grades.txt', 'r') as grades:
grades_read = grades.read()
print(grades_read)
except FileNotFoundError:
print('File not found')