TypeError: Cannot convert 'int' object to str implicitly

I'm trying to write a text game, and I came across an error in the function that I define, which allows you to basically spend your skill points after you create your character. At first, the error showed that I was trying to subtract a string from an integer in this part of the code: balance - strength . Obviously, this was wrong, so I fixed it with strength = int(strength) ... but now I get this error that I have never seen before (new programmer), and I'm stumped that it is he trying to tell me how I correct it.

Here is my code for the part of the function that doesn't work:

 def attributeSelection(): balance = 25 print("Your SP balance is currently 25.") strength = input("How much SP do you want to put into strength?") strength = int(strength) balanceAfterStrength = balance - strength if balanceAfterStrength == 0: print("Your SP balance is now 0.") attributeConfirmation() elif strength < 0: print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!") attributeSelection() elif strength > balance: print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!") attributeSelection() elif balanceAfterStrength > 0 and balanceAfterStrength < 26: print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.") else: print("That is an invalid input. Restarting attribute selection.") attributeSelection() 

And here is the error that I get when I get to this part of the code in the shell:

  Your SP balance is currently 25. How much SP do you want to put into strength?5 Traceback (most recent call last): File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 205, in <module> gender() File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 22, in gender customizationMan() File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 54, in customizationMan characterConfirmation() File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 93, in characterConfirmation characterConfirmation() File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 85, in characterConfirmation attributeSelection() File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 143, in attributeSelection print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.") TypeError: Can't convert 'int' object to str implicitly 

Does anyone know how to solve this? Thanks in advance.

+55
python string int implicit
Nov 30 '12 at 22:34
source share
2 answers

You cannot combine string with int . You will need to convert int to string using the str function or use formatting to format the output.

Edit: -

 print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.") 

to: -

 print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength)) 

or: -

 print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.") 

or according to the comment use , to pass different lines to your print function, and not to concatenate with + : -

 print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.") 
+104
Nov 30 '12 at 22:36
source share
 def attributeSelection(): balance = 25 print("Your SP balance is currently 25.") strength = input("How much SP do you want to put into strength?") balanceAfterStrength = balance - int(strength) if balanceAfterStrength == 0: print("Your SP balance is now 0.") attributeConfirmation() elif strength < 0: print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!") attributeSelection() elif strength > balance: print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!") attributeSelection() elif balanceAfterStrength > 0 and balanceAfterStrength < 26: print("Ok. You're balance is now at " + str(balanceAfterStrength) + " skill points.") else: print("That is an invalid input. Restarting attribute selection.") attributeSelection() 
-one
Mar 24 '18 at 1:44
source share



All Articles