查看: 1812|回复: 2

TypeError an 'int' object cannot be implicitly converted to str

[复制链接]

15

主题

97

帖子

310

积分

论坛管理

Rank: 4

积分
310
发表于 2018-9-19 15:59:11 | 显示全部楼层 |阅读模式
我正在尝试编写一个文本游戏,我在我定义的函数中遇到了一个错误,错误表明我试图从代码的这一部分中的整数中减去一个字符串:balance - strength。显然这是错误的,所以我用strength = int(strength)... 修复它但是现在我得到了这个我以前从未见过的错误(新程序员)。
这是我的函数部分无法正常工作的代码:
  1. def attributeSelection():
  2.     balance = 25
  3.     print("Your SP balance is currently 25.")
  4.     strength = input("How much SP do you want to put into strength?")
  5.     strength = int(strength)
  6.     balanceAfterStrength = balance - strength
  7.     if balanceAfterStrength == 0:
  8.         print("Your SP balance is now 0.")
  9.         attributeConfirmation()
  10.     elif strength < 0:
  11.         print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
  12.         attributeSelection()
  13.     elif strength > balance:
  14.         print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
  15.         attributeSelection()
  16.     elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
  17.         print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
  18.     else:
  19.         print("That is an invalid input. Restarting attribute selection.")
  20.         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
有谁知道如何解决这个问题?谢谢你。
回复

使用道具 举报

22

主题

107

帖子

266

积分

中级会员

Rank: 3Rank: 3

积分
266
发表于 2018-9-19 16:02:17 | 显示全部楼层
不能用int连接一个string。您需要使用str函数将您转换int为str,或使用格式化输出。Stringstrformatting

  1. print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")
复制代码

变成
  1. print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))
复制代码

或者
  1. print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")
复制代码

或者根据注释,用于,将不同的字符串传递给您的print函数,而不是使用+方法连接:
  1. print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
复制代码


回复

使用道具 举报

19

主题

68

帖子

225

积分

论坛管理

Rank: 4

积分
225
发表于 2018-9-19 16:02:51 | 显示全部楼层
  1. def attributeSelection():
  2. balance = 25
  3. print("Your SP balance is currently 25.")
  4. strength = input("How much SP do you want to put into strength?")
  5. balanceAfterStrength = balance - int(strength)
  6. if balanceAfterStrength == 0:
  7.     print("Your SP balance is now 0.")
  8.     attributeConfirmation()
  9. elif strength < 0:
  10.     print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
  11.     attributeSelection()
  12. elif strength > balance:
  13.     print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
  14.     attributeSelection()
  15. elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
  16.     print("Ok. You're balance is now at " + str(balanceAfterStrength) + " skill points.")
  17. else:
  18.     print("That is an invalid input. Restarting attribute selection.")
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表