TypeError:无法隐式地将'int'对象转换为str [英] TypeError: Can't convert 'int' object to str implicitly

查看:336
本文介绍了TypeError:无法隐式地将'int'对象转换为str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个文本游戏,我在我定义的函数中遇到了一个错误,让你在制作角色后基本上可以花费你的技能点数。起初,错误表明我试图从代码的这一部分中的整数中减去一个字符串: balance-strength 。显然这是错的,所以我用 strong = int(strength)修复它......但是现在我收到了这个我以前从未见过的错误(新程序员)和我我很难知道它究竟要告诉我什么以及如何解决它。

I am trying to write a text game and I have run into an error in the function I am defining that lets you basically spend your skill points after you make your character. At first, the error stated that I was attempting to subtract a string from an integer in this part of the code:balance - strength. Obviously that was wrong so I fixed it with strength = int(strength)... but now I am getting this error which I have never seen before(new programmer) and I am stumped on what exactly it is trying to tell me and how I fix it.

这是我的函数部分无法正常工作的代码:

Here is my code for the part of the function that isn't working:

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中代码的这一部分:

And here is the error I get when I get to this part of the code in the 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

有没有人知道怎么解决这个?提前谢谢。

Does anyone know how to solve this? Thanks ahead.

推荐答案

您无法使用<$连接字符串 C $ C> INT 。您需要使用 str int 转换为字符串 c>功能,或使用格式化来格式化输出。

You cannot concatenate a string with an int. You would need to convert your int to a string using the str function, or use formatting to format your output.

更改: -

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

或根据评论,使用将不同的字符串传递给您打印功能,而不是使用 + 连接: -

or as per the comment, use , to pass different strings to your print function, rather than concatenating using +: -

print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")

这篇关于TypeError:无法隐式地将'int'对象转换为str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆