欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python中object转str_Python-TypeError:无法将“ int”对象隐式转换为str

发布时间:2024/9/30 python 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python中object转str_Python-TypeError:无法将“ int”对象隐式转换为str 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

我正在尝试编写文本游戏,但在定义的函数中遇到了错误,该错误使你基本上无法在制作角色后花费技能点。最初,错误表明我正在尝试从代码的这一部分的整数中减去一个字符串balance - strength。显然这是错误的,所以我用strength = int(strength)… 修复了它,但是现在我遇到了一个以前从未见过的错误(新程序员),并且我对确切要告诉我的内容以及如何解决它感到困惑。

这是我的部分功能不起作用的代码:

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中获得这部分代码时遇到的错误:

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

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

有谁知道如何解决这个问题?谢谢你

总结

以上是生活随笔为你收集整理的python中object转str_Python-TypeError:无法将“ int”对象隐式转换为str的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。