Ik probeer een tekstspel te schrijven en ik ben een fout tegengekomen in de functie die ik definieer waarmee je in principe je vaardigheidspunten kunt uitgeven nadat je je personage hebt gemaakt. In eerste instantie gaf de fout aan dat ik probeerde een string af te trekken van een geheel getal in dit deel van de code:balance - strength
. Dat was duidelijk verkeerd, dus ik heb het opgelost met strength = int(strength)
… maar nu krijg ik deze fout die ik nog nooit eerder heb gezien (nieuwe programmeur) en ik ben stomverbaasd over wat het precies is probeert me te vertellen en hoe ik het kan oplossen.
Hier is mijn code voor het deel van de functie dat niet werkt:
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()
En hier is de foutmelding die ik krijg als ik bij dit deel van de code in de shell kom:
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
Weet iemand hoe dit op te lossen? Bedankt alvast.
Antwoord 1, autoriteit 100%
Je kunt een string
niet samenvoegen met een int
. U moet uw int
converteren naar een string
met behulp van de functie str
, of formatting
gebruiken om uw uitvoer op te maken .
Wijzigen: –
print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")
naar: –
print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))
of: –
print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")
of volgens de opmerking, gebruik ,
om verschillende tekenreeksen door te geven aan uw print
-functie, in plaats van samen te voegen met +
: –
print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
Antwoord 2
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()