Skip to content
Advertisement

Unsupported operand type(s) for +: ‘float’ and ‘str’ error [closed]

I am trying to add the contents of the score file together and get an average but I can’t seem to get it to work.

My code:

JavaScript

The score file is full of float numbers:

JavaScript

The error message

JavaScript

Advertisement

Answer

Since score was created by splitting a string, it’s elements are all strings; hence the complaint about trying to add a float to a string. If you want the value that that string represents, you need to compute that; something like float(score[i]).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement