类型错误:无法隐式将'int'对象转换为'str'

72
我正在尝试编写一个文本游戏,在定义函数时遇到了一个错误,该函数允许您在创建角色后基本上花费技能点数。起初,这个错误指出我在代码的这一部分尝试从整数中减去一个字符串: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的一条座右铭是“明确优于隐晦”。 - Name McChange
我知道这与问题完全无关,但在从“余额”中扣除后进行的“强度>平衡”检查意味着您只能在强度上花费不超过余额的一半。这是有意为之还是一个错误?(顺便说一句,“好的。你的余额现在是“——你的”,而不是“你是”,你也不需要“在”这个词。) - abarnert
我知道它还存在很多问题,我还需要进行大量的调试工作。 - anon
2个回答

131
你不能将一个字符串和整数拼接在一起。你需要使用 str 函数将你的整数转换为字符串,或者使用格式化来格式化你的输出。
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("好的。你现在有", str, "点技能点"),而不是试图添加内容。 - abarnert
1
@abarnert.. 是的,这比 + 更好。 - Rohit Jain
6
但是根据你所写的编辑,它会造成误导。你不能使用逗号来连接字符串;你可以使用逗号将参数分隔开并传给 print 函数,这些参数将一个接一个地打印出来,并在它们之间添加空格。 - abarnert
1
@TylerHaddaway.. 当然你不能将 "abc" 转换为整数。但是,你可以使用 int("12")"12" 转换为整数。 - Rohit Jain
@abarnert.. 哎呀!我对此感到困惑。我并不意外,因为我在 Python 方面还有许多要学习的地方。只是一个初学者而已。;) - Rohit Jain
显示剩余9条评论

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接