首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:无法将“int”对象隐式转换为字符串

TypeError:无法将“int”对象隐式转换为字符串
EN

Stack Overflow用户
提问于 2012-12-01 06:34:06
回答 2查看 239.6K关注 0票数 71

我正在尝试写一个文本游戏,我在我定义的函数中遇到了一个错误,这个函数让你在制作角色后基本上花掉你的技能点数。一开始,错误指出我试图从这部分代码中的整数中减去一个字符串:balance - strength。显然这是错误的,所以我用strength = int(strength)修复了它。但是现在我得到了这个我以前从未见过的错误(新程序员),我被它试图告诉我的到底是什么以及我如何修复它难住了。

下面是我的代码,用于处理函数不起作用的部分:

代码语言:javascript
复制
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中这部分代码时得到的错误:

代码语言:javascript
复制
    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

有人知道怎么解决这个问题吗?谢谢您的支持。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-01 06:36:26

不能将stringint连接起来。您需要使用str函数将int转换为string,或者使用formatting格式化输出。

更改:-

代码语言:javascript
复制
print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")

致:

代码语言:javascript
复制
print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))

或:

代码语言:javascript
复制
print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")

或者根据注释,使用,将不同的字符串传递给print函数,而不是使用+进行连接:-

代码语言:javascript
复制
print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
票数 130
EN

Stack Overflow用户

发布于 2018-03-24 09:44:44

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13654168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档