Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - TypeError: Can't convert 'int' object to str implicitly

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:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 205, in <module>
    gender()
  File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 22, in gender
    customizationMan()
  File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 54, in customizationMan
    characterConfirmation()
  File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 93, in characterConfirmation
    characterConfirmation()
  File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 85, in characterConfirmation
    attributeSelection()
  File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...