71

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

有谁知道如何解决这个问题?提前谢谢。

4

2 回答 2

130

您不能将 astring与连接起来int。您需要使用该函数将您的转换int为 a ,或用于格式化您的输出。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.")
于 2012-11-30T22:36:25.903 回答
0
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()
于 2018-03-24T01:44:44.100 回答