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

前端 未结 2 1998
北海茫月
北海茫月 2020-11-22 11:51

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

相关标签:
2条回答
  • 2020-11-22 11:56
    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()
    
    0 讨论(0)
  • 2020-11-22 11:57

    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.")
    
    0 讨论(0)
提交回复
热议问题