str - tipos de variables en python
TypeError: no se puede convertir el objeto ''int'' en str implícitamente (2)
Intento escribir un juego de texto y me encontré con un error en la función que estoy definiendo, que básicamente te permite gastar tus puntos de habilidad después de hacer tu personaje. Al principio, el error decía que estaba intentando restar una cadena de un entero en esta parte del código: balance - strength
. Obviamente eso estaba mal, así que lo solucioné con strength = int(strength)
... pero ahora recibo este error que nunca había visto antes (nuevo programador) y estoy perplejo sobre qué es exactamente lo que está tratando de decirme y cómo Lo arreglo.
Aquí está mi código para la parte de la función que no está funcionando:
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()
Y aquí está el error que recibo cuando llego a esta parte del código en el 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
Alguien sabe cómo resolver esto? Gracias por delante
No puede concatenar una string
con un int
. Necesitarás convertir tu int
a una string
usando la función str
, o usar el formatting
para formatear tu salida.
Cambio: -
print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")
a: -
print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))
o:
print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")
o según el comentario, use para pasar diferentes cadenas a su función de print
, en lugar de concatenar usando +
: -
print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
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()