TypeError:無法將‘int’對象隱式轉(zhuǎn)換為str我正在嘗試寫一個文字游戲,我在我定義的函數(shù)中遇到了一個錯誤,這個函數(shù)讓你在完成角色后基本上可以使用你的技能點。首先,錯誤聲明我試圖從代碼的這一部分中的整數(shù)中減去一個字符串:balance - strength..顯然那是錯誤的,所以我用strength = int(strength)..但是現(xiàn)在我得到了這個錯誤,這是我以前從未見過的(新程序員),我很困惑它到底想告訴我什么,以及我是如何修復它的。下面是函數(shù)中不起作用的部分的代碼: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()有人道怎么解決這個問題嗎?先謝了。
2 回答

慕斯709654
TA貢獻1840條經(jīng)驗 獲得超5個贊
def attributeSelection():balance = 25print("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/150
提交
取消