【问题标题】:TypeError: Can't convert 'int' object to str implicitlyTypeError:无法将“int”对象隐式转换为str
【发布时间】:2012-11-19 04:51:05
【问题描述】:

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

有谁知道如何解决这个问题?先谢谢了。

【问题讨论】:

  • 你必须这样做 str(balanceAfterStrength) 因为 Python 的座右铭之一是“显式优于隐式”
  • 我知道这与问题完全无关,但是从balance 中减去后的strength &gt; balance 检查意味着您最多只能将余额的一半用于强度。这是故意的,还是bug? (而且,当我选择无关紧要的内容时,“好吧。你的余额现在是“-“你的”而不是“你是”,而且你不需要“在”。)
  • 我知道它有很多问题,我还有很多调试工作要做。

标签: python string int implicit


【解决方案1】:

您不能将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.")

【讨论】:

  • 或者,而不是尝试添加,print("Ok. You're balance is now at", str, "skill points")
  • @abarnert.. 是的,这比+ 好。
  • 但是当您的编辑被写入时,它具有误导性。您不能使用, 连接字符串;您可以使用, 分隔print 的参数,这些参数将一一打印,它们之间有空格。
  • 好的,我要将我的代码更改为您建议 Rohit 的最后一种格式。感谢您的帮助!
  • @TylerHaddaway.. 当然你不能将"abc" 转换为整数。但是,是的,您可以使用int("12")"12" 转换为整数。
【解决方案2】:
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 2017-07-21
    • 1970-01-01
    • 2017-08-31
    • 2015-12-23
    相关资源
    最近更新 更多