相关疑难解决方法(0)

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 …
Run Code Online (Sandbox Code Playgroud)

python string int implicit

62
推荐指数
1
解决办法
21万
查看次数

如何连接str和int对象?

如果我尝试执行以下操作:

things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)

我在Python 3.x中收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)

......以及Python 2.x中的类似错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

python string concatenation python-2.x python-3.x

60
推荐指数
2
解决办法
8万
查看次数

标签 统计

python ×2

string ×2

concatenation ×1

implicit ×1

int ×1

python-2.x ×1

python-3.x ×1