+11 votes
1 view
in Python by (43k points)

I am trying to execute the following python code

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()

And it gives me the following error:

 Your SP balance is currently 25.

How much SP do you want to put into a 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

What is the issue?

1 Answer

+10 votes
by (92.1k points)
edited by

The point is that you cannot concatenate a string and an integer. Convert the integer explicitly yo string and then try concatenating. Perform the following command to explicitly convert the integer to string:

print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))

To know more about this you can have a look at the following video:-

Related questions

+12 votes
1 answer
+2 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...