问题在于:创建一个Python程序,允许用户输入每个学生所获得的班级名称,教师姓名,每个学生的姓名和字母等级。该计划应计算并显示获得As,Bs,Cs,Ds和Fs的学生人数。程序应将所有输入和输出信息保存到名为ClassNameGrades.txt的文本文件中。
我到目前为止编写的代码如下。我只是在学习,这是非常简陋的,我道歉。下面是输出。我不知道为什么我会弹出2个额外的记录。有人可以向我解释我做错了什么吗?创建文件,输入被写入文件,但在读取期间它会崩溃。
def main():
student_num = 0
count = 0
a_total = 0
b_total = 0
c_total = 0
d_total = 0
f_total = 0
student_num = int(input("Enter the number of students you wish to enter
grades for: "))
gradefile = open('gradebook.txt', 'w')
for count in range(1, student_num + 1):
class_name = input('Enter class name: ')
faculty_name = input('Enter faculty name: ')
student_name = input('Enter student name: ')
letter_grade = input('Enter letter grade (A, B, C, D, or F) ')
print()
if letter_grade.upper() == 'A':
a_total += 1
elif letter_grade.upper() == 'B':
b_total += 1
elif letter_grade.upper() == 'C':
c_total += 1
elif letter_grade.upper() == 'D':
d_total += 1
elif letter_grade.upper() == 'F':
f_total += 1
else:
print('Invalid response')
gradefile.write(class_name + '\n')
gradefile.write(faculty_name + '\n')
gradefile.write(student_name + '\n')
gradefile.write(letter_grade + '\n')
gradefile.write(str(a_total) + '\n')
gradefile.write(str(b_total) + '\n')
gradefile.write(str(c_total) + '\n')
gradefile.write(str(d_total) + '\n')
gradefile.write(str(f_total) + '\n')
gradefile.close()
gradefile1 = open('gradebook.txt', 'r')
class_name = gradefile1.readline()
while class_name != '':
faculty_name = gradefile1.readline()
student_name = gradefile1.readline()
letter_grade = gradefile1.readline()
class_name = class_name.rstrip('\n')
faculty_name = faculty_name.rstrip('\n')
student_name = student_name.rstrip('\n')
letter_grade = letter_grade.rstrip('\n')
print('Class name: ', class_name)
print('Faculty name: ', faculty_name)
print('Student name: ', student_name)
print('Letter grade: ', letter_grade.upper())
print()
class_name = gradefile1.readline()
a_total = gradefile1.readline()
b_total = gradefile1.readline()
c_total = gradefile1.readline()
d_total = gradefile1.readline()
f_total = gradefile1.readline()
a_total = a_total.rstrip('\n')
b_total = b_total.rstrip('\n')
c_total = c_total.rstrip('\n')
d_total = d_total.rstrip('\n')
f_total = f_total.rstrip('\n')
print('Total As: ', a_total)
print('Total Bs: ', b_total)
print('Total Cs: ', c_total)
print('Total Ds: ', d_total)
print('Total Fs: ', f_total)
gradefile1.close()
main()
用户输入:
Enter the number of students you wish to enter grades for: 3
Enter class name: bio
Enter faculty name: jones
Enter student name: smith
Enter letter grade (A, B, C, D, or F) A
Enter class name: bio
Enter faculty name: jones
Enter student name: gray
Enter letter grade (A, B, C, D, or F) B
Enter class name: Bio
Enter faculty name: Jones
Enter student name: Thomas
Enter letter grade (A, B, C, D, or F) B
返回输出:
Class name: bio
Faculty name: jones
Student name: smith
Letter grade: A
Class name: bio
Faculty name: jones
Student name: gray
Letter grade: B
Class name: Bio
Faculty name: Jones
Student name: Thomas
Letter grade: B
Class name: 1
Faculty name: 2
Student name: 0
Letter grade: 0
Class name: 0
Faculty name:
Student name:
Letter grade:
Total As:
Total Bs:
Total Cs:
Total Ds:
Total Fs:
Process finished with exit code 0
答案 0 :(得分:0)
虽然我现在不与我的开发人员在一起,看看你的代码,但我认为问题在于for语句。而不是范围(1,student_num + 1)尝试重写它,所以你改为使用student_num输入值作为迭代量。基于你编写它的方式,我相信正在发生的事情是for循环是因为student_num + 1而错误地计算迭代。如果用户为student_num输入3,那么3应该是指定的唯一迭代量。
答案 1 :(得分:0)
当您阅读文件时,您可以在学生数据和总计数据之间找到一个空白行,但您还没有写过。
因此,在你整个文件re之前,while class_name != ''
并不是假的。
在两者之间添加一个空白行,您的代码将起作用。
[...]
gradefile.write('\n')
gradefile.write(str(a_total) + '\n')