TypeError:无法将“ int”对象隐式转换为str


69

我正在尝试编写文本游戏,但是在定义的函数中遇到了一个错误,该错误使您基本上无法在制作角色后花费技能点。最初,该错误表明我正在尝试从代码的这一部分的整数中减去一个字符串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

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


12
您必须这样做,str(balanceAfterStrength)因为Python的座右铭之一是“显式胜于隐式”
McChange命名为2012年

我知道这与问题完全无关,但是strength > balance减去的检查balance意味着您最多只能将一半的精力花费在力量上。这是故意的还是错误?(而且,当我选择不相关的尼特时,“好吧。您的余额现在是“ —”您的”而不是“您是”,并且您不需要“ at”。)
abarnert 2012年

我知道它有很多问题,我还有很多调试工作要做。
2012年

Answers:


126

您不能将链接stringint。您需要使用函数将转换int为,或使用格式化输出。stringstrformatting

变更:-

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.")

1
或者,而不是尝试添加print("Ok. You're balance is now at", str, "skill points")
abarnert,2012年

1
@abarnert ..是的,那比+
罗希特·贾因

6
但是,由于您撰写的编辑内容具有误导性。您不能使用,串联字符串;您可以使用,分隔参数print,这些参数将一一打印,并在它们之间留有空格。
abarnert 2012年

好的,我将代码更改为您建议的Rohit的最后一种格式。谢谢您的帮助!
2012年

1
@TylerHaddaway ..当然您不能转换"abc"为整数。但是可以,您可以确定"12"使用转换为整数int("12")
罗希特·贾因

0
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()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.