72

I am trying to write a text game and I have run into an error in the function I am defining that lets you basically spend your skill points after you make your character. At first, the error stated that I was attempting to subtract a string from an integer in this part of the code:balance - strength. Obviously that was wrong so I fixed it with strength = int(strength)... but now I am getting this error which I have never seen before(new programmer) and I am stumped on what exactly it is trying to tell me and how I fix it.

Here is my code for the part of the function that isn't working:

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 here is the error I get when I get to this part of the code in the 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

Does anyone know how to solve this? Thanks ahead.

anon
  • 1,407
  • 3
  • 14
  • 12
  • 12
    You must do `str(balanceAfterStrength)` because one of Python's mottos is "Explicit is better than Implicit" – Name McChange Nov 30 '12 at 22:35
  • I know this is completely irrelevant to the problem, but that `strength > balance` check after subtracting from `balance` means you can only spend up to half your balance on strength. Is that intentional, or a bug? (And, while I'm picking irrelevant nits, "Ok. Your balance is now "—"Your" not "You're", and you don't need the "at".) – abarnert Nov 30 '12 at 22:50
  • I know there are a lot of problems with it, I still have a lot of debugging to do. – anon Dec 01 '12 at 09:35

2 Answers2

131

You cannot concatenate a string with an int. You would need to convert your int to a string using the str function, or use formatting to format your output.

Change: -

print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")

to: -

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

or: -

print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")

or as per the comment, use , to pass different strings to your print function, rather than concatenating using +: -

print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
Evelyn Marie
  • 368
  • 4
  • 6
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    Or, instead of trying to add, `print("Ok. You're balance is now at", str, "skill points")`. – abarnert Nov 30 '12 at 22:47
  • 1
    @abarnert.. Yeah, that is better than `+`. – Rohit Jain Nov 30 '12 at 22:48
  • 6
    But as your edit is written, it's misleading. You cannot use `,` to concatenate strings; you can use `,` to separate arguments to `print`, which will be printed one by one, with spaces between them. – abarnert Nov 30 '12 at 22:51
  • Ok, I am going to change my code to the last format you suggested Rohit. Thanks for the help! – anon Nov 30 '12 at 22:52
  • @abarnert. Oh. Is it like that? I thought `,` is also used for concatenation in Python. – Rohit Jain Nov 30 '12 at 22:52
  • No, `,` is not used for concatenation in Python, just for separating function arguments (and tuple members and various other things). In fact, of the reasons that Python 3 has a `print` function instead of a `print` statement like Python 2 was that the magical behavior of `,` in `print` used to mislead people in exactly that way. – abarnert Nov 30 '12 at 22:55
  • @abarnert. Ok. I got to know something new today. Thanks :) And yes, since I use `Python 2`, I too might have been misleaded there. ;) – Rohit Jain Nov 30 '12 at 22:57
  • One last point: the `,` does _not_ require `str(balance)`; that's the whole reason I suggested it. The `print` function can print any type, including `int`, so if you make the string and integer values separate parameters, you don't need to convert everything into strings. Also, `print` automatically adds spaces, so you don't want to add those extra spaces yourself. Finally, in Python 2, you need to remove the parentheses, because it'll end up printing a `tuple` (with explicit parentheses and commas). – abarnert Nov 30 '12 at 22:57
  • Oh, one more question, sorry to ask so late. Is it possible for me to change the string to an integer rather than the integer to a string? – anon Nov 30 '12 at 22:58
  • In short: `print "Ok. You're balance is now at", balanceAfterStrength, "skill points."` for Python 2, or `print("Ok. You're balance is now at", balanceAfterStrength, "skill points.")` for Python 3. – abarnert Nov 30 '12 at 22:59
  • 1
    @TylerHaddaway.. Of course you can't convert `"abc"` to an integer. But yes, you can sure convert `"12"` to intege using `int("12")`. – Rohit Jain Nov 30 '12 at 23:01
  • @RohitJain: Actually, you *can* convert `"abc"` to an integer in any base > 12: `int("abc", 16)` is `2748`. But you probably don't actually *want* to convert `"abc"` to an integer if you didn't already know that, so your answer is good. – abarnert Nov 30 '12 at 23:03
  • Yeah, I don't plan to turn letters into a number anytime soon, lol. – anon Nov 30 '12 at 23:05
  • @abarnert.. Aww! I'm stumped with that. Well, I'm not surprised, because I have still much to learn in Python. Just a beginner in it. ;) – Rohit Jain Nov 30 '12 at 23:07
0
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()