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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值