小编Usm*_*han的帖子

+的'不支持的操作数类型:'float'和'str'错误

我是Python的新手,我现在仍然坚持做什么,因为我不断收到这个错误.我试图将得分文件的内容添加到一起并获得平均值,但我似乎无法让它工作.

我的代码:

# open and read file student / score
student_file = open("Student.txt", "r")
score_file = open("Score.txt", "r")
student = student_file.read().split(' ')
score = score_file.read().split(' ')
addedScore = 0.0
average = 0.0

for i in range(0,len(student)):

    print("Student: "+student[i]+" Final: "+score[i])          
    addedScore = addedScore + score[i]

average = addedScore / 2
print("The class average is:", average)
Run Code Online (Sandbox Code Playgroud)

分数文件充满了浮点数:

90.0 94.0 74.4 63.2 79.4 87.6 67.7 78.1 95.8 82.1
Run Code Online (Sandbox Code Playgroud)

错误消息

line 12, in <module>
addedScore = addedScore + score[i]
TypeError: unsupported operand type(s) …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
3万
查看次数

标签 统计

python ×1