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()有人道怎么解决这个问题吗?先谢了。
2 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
def attributeSelection():balance = 25print("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()
添加回答
举报
0/150
提交
取消