TypeError:无法将‘int’对象隐式转换为str

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()


有人道怎么解决这个问题吗?先谢了。


qq_遁去的一_1
浏览 552回答 2
2回答

慕斯709654

def&nbsp;attributeSelection():balance&nbsp;=&nbsp;25print("Your&nbsp;SP&nbsp;balance&nbsp;is&nbsp;currently&nbsp;25.")strength&nbsp;=&nbsp;input("How&nbsp;much&nbsp;SP&nbsp;do&nbsp;you&nbsp;want&nbsp;to&nbsp;put&nbsp;into&nbsp;strength?") balanceAfterStrength&nbsp;=&nbsp;balance&nbsp;-&nbsp;int(strength)if&nbsp;balanceAfterStrength&nbsp;==&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print("Your&nbsp;SP&nbsp;balance&nbsp;is&nbsp;now&nbsp;0.") &nbsp;&nbsp;&nbsp;&nbsp;attributeConfirmation()elif&nbsp;strength&nbsp;<&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print("That&nbsp;is&nbsp;an&nbsp;invalid&nbsp;input.&nbsp;Restarting&nbsp;attribute&nbsp;selection.&nbsp;Keep&nbsp;an&nbsp;eye&nbsp;on&nbsp;your&nbsp;balance&nbsp;this&nbsp;time!") &nbsp;&nbsp;&nbsp;&nbsp;attributeSelection()elif&nbsp;strength&nbsp;>&nbsp;balance: &nbsp;&nbsp;&nbsp;&nbsp;print("That&nbsp;is&nbsp;an&nbsp;invalid&nbsp;input.&nbsp;Restarting&nbsp;attribute&nbsp;selection.&nbsp;Keep&nbsp;an&nbsp;eye&nbsp;on&nbsp;your&nbsp;balance&nbsp;this&nbsp;time!") &nbsp;&nbsp;&nbsp;&nbsp;attributeSelection()elif&nbsp;balanceAfterStrength&nbsp;>&nbsp;0&nbsp;and&nbsp;balanceAfterStrength&nbsp;<&nbsp;26: &nbsp;&nbsp;&nbsp;&nbsp;print("Ok.&nbsp;You're&nbsp;balance&nbsp;is&nbsp;now&nbsp;at&nbsp;"&nbsp;+&nbsp;str(balanceAfterStrength)&nbsp;+&nbsp;"&nbsp;skill&nbsp;points.")else: &nbsp;&nbsp;&nbsp;&nbsp;print("That&nbsp;is&nbsp;an&nbsp;invalid&nbsp;input.&nbsp;Restarting&nbsp;attribute&nbsp;selection.") &nbsp;&nbsp;&nbsp;&nbsp;attributeSelection()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python