当前位置: 首页 > 面试题库 >

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

徐安康
2023-03-14
问题内容

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

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


问题答案:

你不能将链接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.")


 类似资料:
  • 问题内容: 我正在尝试测试某个数字的十进制表示形式是否至少包含两次数字9,所以我决定执行以下操作: 但是Python始终会以“ TypeError:无法将’int’对象隐式转换为str”进行响应。 我在这里做错了什么?实际上,是否存在一种更聪明的方法来检测整数的十进制表示形式中包含某个数字的频率? 问题答案: 您的问题在这里: 您需要使字符串文字,而不是整数: 为了更好地计算字符串中出现的次数,请

  • 问题内容: 我正在学习pythonthehardway中进行练习41,并不断收到错误: 我使用的是python3,而书籍使用的是python2,因此我进行了一些更改。这是脚本: 我到底在做什么错?谢谢! 问题答案: 返回一个bytes对象,要对其执行字符串操作,应将其转换为first。

  • 我试图检查是否为。但我得到了一个错误: 无法将null转换为对象 我该如何进行? 我正在检查组和用户是否存在,否则将添加它们。

  • 我想在现场放置六个物体(球)。我认为代码看起来可行,但我收到一个控制台消息。信息: “Assets/GameScripts/Instance.cs(26,40):错误CS0266:无法将typeUnityEngine.Vector3”。存在显式转换(是否缺少转换?) 使用UnityEngine;使用系统。收藏; 公共类实例:单行为{公共游戏对象球; }

  • 问题内容: 我将开始处理需要读取字节并创建字符串的内容。读取的字节代表UTF-16字符串。因此,为了进行测试,我想将UTF-16编码的简单字节数组转换为字符串。数组中的前2个字节必须表示字节序,因此必须为0xff 0xfe或0xfe 0xff。所以我尝试如下创建字节数组: 但是我收到一个错误,因为0xFF和0xFE太大而无法容纳一个字节(因为字节是用Java签名的)。更准确地说,错误是int无法转

  • 所以,问题是--为什么在I=L的情况下,当L的大小足够小到适合I时,必须显式地进行,但在F=L的情况下,当L的大小也适合F时,铸造可以隐式地进行,而不会产生错误。 我的意思是,在这两种情况下,右操作数的大小可能不适合左操作数。那么,为什么在一种情况下(I=L),强制转换必须显式进行,而在另一种情况下(F=L)可以隐式进行呢?虽然隐式地将长var转换为int var比隐式地将长var转换为浮点var