我正在尝试编写一个文本游戏,在定义函数时遇到了一个错误,该函数允许您在创建角色后基本上花费技能点数。起初,这个错误指出我在代码的这一部分尝试从整数中减去一个字符串:
以下是我定义的函数部分代码:
有人知道如何解决这个问题吗?提前谢谢。
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
有人知道如何解决这个问题吗?提前谢谢。
str(balanceAfterStrength)
,因为Python的一条座右铭是“明确优于隐晦”。 - Name McChange