TypeError: Can't convert 'int' object to str implicitly
我在写一个文本游戏,我在我定义的函数中遇到了一个错误,这个函数让你基本上在塑造你的角色后花掉你的技能点。起初,错误表明我正试图从代码的这部分整数中减去一个字符串:
以下是我对不起作用的函数部分的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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() |
下面是我在shell中得到代码的这一部分时得到的错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 |
有人知道怎么解决这个问题吗?谢谢。
不能将
变化:
1 | print("Ok. Your balance is now at" + balanceAfterStrength +" skill points.") |
至:
1 | print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength)) |
或:
1 | print("Ok. Your balance is now at" + str(balanceAfterStrength) +" skill points.") |
或者根据注释,使用
1 | print("Ok. Your balance is now at", balanceAfterStrength," skill points.") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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() |