Reputation: 13
I currently have a bit of code that adds a certain amount of points to a plyers score:
points = 5
player1 = 0
float(points)
float(player1)
player1 += points
points = 0
The problem is when I run this I get this error:
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
At first, I thought it had to do with the data types of the variables, points and player1 but I have a float function right before it. Also just for debugging purposes I wrote a bool
statement print(bool(float(var)))
to check if it was floating correct and it wasn't. The points variable floated correctly but it was saying that player1
was not a float
. Any ideas on why this is happening?
Upvotes: 0
Views: 65
Reputation: 31
First of all, I was not able to reproduce the error, meaning I executed your code and did not get the error.
Second: When you cast a variable as: float(points) you must save the casting in a variable (or update the previous one).
points = float(points)
Note: in your case, you don't need to cast the variable to float because you are using only int values.
Upvotes: 0
Reputation: 89264
If you are reading user input, you need to assign the result of float
to the variable.
points = float(points) # convert existing string, or use float(input())
player1 = float(player1)
Upvotes: 2