python - 为什么我的 python 程序没有给出正确的文件输出?
问题描述
您好,我的作业有问题...我不断收到一个错误,我将在我的程序之后发布。
我的程序
def create():
myGrades_file = open('grades.txt','w')
while True:
my_courses = input("Enter course name or Enter to quit: ")
if my_courses == '':
print("File was created and closed")
break
my_grades = int(input("Enter grade (integer) achieved: "))
string = my_courses+','+" "+str(my_grades)+'\n'
myGrades_file.write(string)
myGrades_file.close()
return True
def retrieve():
print("\nHere are your grades:")
grades_total = 0
count = 0
gpa=0
myGrades_file = open('grades.txt','r')
for line in myGrades_file:
mycourses, grades = line.strip().split(',')
grades_total+=int(grades)
if(int(grades)>=90):
gpa+=4.0
elif(int(grades)>=80):
gpa+=3.0
elif(int(grades)>=70):
gpa+=2.0
elif(int(grades)>=60):
gpa+=1.0
else:
gpa+=0.0
count+=1
gpa_total = gpa/count
print("{} score is{}".format(mycourses,grades))
average = grades_total/count
print("Average grade among your courses is {:.2f}".format(average),"Your GPA is {:.2f}".format(gpa_total))
return average
def main():
create()
average = retrieve()
main()
由于某种原因,我无法弄清楚我做错了什么!
错误
Test 2 Expected data: ['CGS1070', '98'] Data in file: ['CGS1070, 98']Test 2 FailedE
======================================================================
ERROR: test_2_io_case_2 (__main__.programTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
AssertionError: Lists differ: ['CGS1070, 98'] != ['CGS1070', '98']
First differing element 0:
'CGS1070, 98'
'CGS1070'
Second list contains 1 additional elements.
First extra element 1:
'98'
- ['CGS1070, 98']
+ ['CGS1070', '98']
? + +
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
ValueError: Contents of file != Expected
解决方案
推荐阅读
- java - 在 junit5 上使用测试套件测试多个类待定
- regex - 查找不以 " 开头的字符串的正则表达式
- java - 在 Eclipse 中打开 Liberty:发布导致 JMX 连接错误
- git - Jenkins 2 Git Webhook“请求中没有包含有效的面包屑”
- python - 使用内核平滑二维 Numpy 数组
- reactjs - 无法使用道具更新状态
- javascript - 无法绑定到“matMenuTrigger”,因为它不是“a”的已知属性
- pandas - 如何修复 tmp 目录中的 Python 2.7 和 Numpy 语法错误
- python-3.x - 无法在 Spyder 或 Python 中导入 TensorFlow(ModuleNotFoundError: No module named 'tensorflow')
- firebase - 如何在 Firebase Flutter 中删除购物清单上的项目