TypeError:无法隐式地将'int'对象转换为str

ano*_*non 62 python string int implicit

我正在尝试编写一个文本游戏,我在我定义的函数中遇到了一个错误,让你在制作角色后基本上可以花费你的技能点数.起初,错误表明我试图从代码的这一部分中的整数中减去一个字符串: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()
Run Code Online (Sandbox Code Playgroud)

这是我在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 <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
Run Code Online (Sandbox Code Playgroud)

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

Roh*_*ain 114

不能连接一个stringint.您需要使用该函数将您转换int为a ,或使用格式化输出.stringstrformatting

变化: -

print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")
Run Code Online (Sandbox Code Playgroud)

至: -

print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))
Run Code Online (Sandbox Code Playgroud)

要么: -

print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")
Run Code Online (Sandbox Code Playgroud)

或者根据注释,用于,将不同的字符串传递给您的print函数,而不是使用+以下连接:

print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
Run Code Online (Sandbox Code Playgroud)

  • 但是当你的编辑被写入时,它会产生误导.你不能用`,`来连接字符串; 您可以使用`,`将参数分隔为'print`,它将逐个打印,并在它们之间留有空格. (6认同)