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

Usm*_*han 5 python

我是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) for +: 'float' and 'str'
Run Code Online (Sandbox Code Playgroud)

我很感激能得到的所有帮助.非常感谢

Sco*_*ter 6

由于score是通过拆分字符串创建的,因此它的元素都是字符串; 因此,有关尝试向字符串添加浮点数的投诉.如果你想要那个字符串代表的值,你需要计算它; 类似的东西float(score[i]).