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

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

253809 次浏览

不能将 stringint连接起来。您需要使用 str函数将 int转换为 string,或者使用 formatting格式化输出。

更改:-

print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")

致:-

print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))

或:-

print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")

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

print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
def attributeSelection():
balance = 25
print("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()