0

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?

3
  • 1
    float(points) doesn't change points. It should be points = float(points) Commented Oct 7, 2021 at 18:10
  • 4
    That error can't happen with the code you showed. points = 5 makes it an int, not str. Commented Oct 7, 2021 at 18:10
  • 1
    This code doesn't have any string variables. Show us the real code. Commented Oct 7, 2021 at 18:17

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I'm stupid can't believe I forgot this, thanks a lot.
0

First of all, I was not able to reproduce the error, meaning I executed your code and did not get the error.

All good (using Programiz)

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.