Unsupported operand type(s) for +: 'float' and 'str' error

Usman Khan

I am new to Python and am stuck with what to do right now because I keep getting this error. 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:

# 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)

The score file is full of float numbers:

90.0 94.0 74.4 63.2 79.4 87.6 67.7 78.1 95.8 82.1

The error message

line 12, in <module>
addedScore = addedScore + score[i]
TypeError: unsupported operand type(s) for +: 'float' and 'str'

I appreciate all the help I can get. Thanks a lot

Scott Hunter

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]).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unsupported operand types for //: 'str', 'int'

From Dev

unsupported operand types for * : 'float' and 'decimal'

From Dev

Two floats throw TypeError: unsupported operand type(s) for +: 'float' and 'str'

From Dev

Barplot returns unsupported operand type(s) for -: 'str' and 'float'

From Dev

Unsupported operand types 'NoneType'

From Dev

Django unsupported operand types

From Dev

TypeError: Can't convert 'float' object to str implicitly or TypeError: unsupported operand type(s) for -: 'str' and 'float'

From Dev

Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

From Dev

Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

From Dev

python error; unsupported operand type(s) for +: 'int' and 'str'

From Dev

getting error unsupported operand type(s) for +: 'int' and 'str'

From Dev

Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

From Dev

Unsupported operand types error when declaring default params as INF

From Dev

Why following codes shows Unsupported operand types error?

From Dev

Type error: unsupported operand

From Dev

Python Type Error Unsupported Operand

From Dev

Unsupported operand type(s) for +: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

TypeError: unsupported operand type(s) for |: 'int' and 'str'

From Dev

Python - Unsupported Operand Type(s) for -: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

From Dev

TypeError: unsupported operand type(s) for +: 'Tag' and 'str'

From Dev

Unsupported operand type(s) for +: 'NoneType' and 'str' and 'int'

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'str'

From Dev

unsupported operand type(s) for /: 'str' and 'int' but different

From Dev

unsupported operand type(s) for +=: 'int' and 'str' on pycharm

From Dev

TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

From Dev

TypeError: unsupported operand type(s) for -: 'float' and 'method'

Related Related

  1. 1

    unsupported operand types for //: 'str', 'int'

  2. 2

    unsupported operand types for * : 'float' and 'decimal'

  3. 3

    Two floats throw TypeError: unsupported operand type(s) for +: 'float' and 'str'

  4. 4

    Barplot returns unsupported operand type(s) for -: 'str' and 'float'

  5. 5

    Unsupported operand types 'NoneType'

  6. 6

    Django unsupported operand types

  7. 7

    TypeError: Can't convert 'float' object to str implicitly or TypeError: unsupported operand type(s) for -: 'str' and 'float'

  8. 8

    Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

  9. 9

    Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

  10. 10

    python error; unsupported operand type(s) for +: 'int' and 'str'

  11. 11

    getting error unsupported operand type(s) for +: 'int' and 'str'

  12. 12

    Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

  13. 13

    Unsupported operand types error when declaring default params as INF

  14. 14

    Why following codes shows Unsupported operand types error?

  15. 15

    Type error: unsupported operand

  16. 16

    Python Type Error Unsupported Operand

  17. 17

    Unsupported operand type(s) for +: 'int' and 'str'

  18. 18

    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  19. 19

    TypeError: unsupported operand type(s) for |: 'int' and 'str'

  20. 20

    Python - Unsupported Operand Type(s) for -: 'int' and 'str'

  21. 21

    TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

  22. 22

    TypeError: unsupported operand type(s) for +: 'Tag' and 'str'

  23. 23

    Unsupported operand type(s) for +: 'NoneType' and 'str' and 'int'

  24. 24

    TypeError: unsupported operand type(s) for +=: 'int' and 'str'

  25. 25

    unsupported operand type(s) for /: 'str' and 'int' but different

  26. 26

    unsupported operand type(s) for +=: 'int' and 'str' on pycharm

  27. 27

    TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

  28. 28

    TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

  29. 29

    TypeError: unsupported operand type(s) for -: 'float' and 'method'

HotTag

Archive