简体   繁体   中英

I am confused about this error

I keep getting this error

TypeError: unsupported operand type(s) for +: 'int' and 'str'   

In my below code:

done = False
while not done:
    if You.Hit_points > 0 and Opponent.Hit_points > 0:
        move = raw_input("Would you like to make a move? (y/n) ")
        if move == "y":
            print "",You.name,"hit ",Opponent.name," by",You.Hit_points," hit points!"
            Opponent.Health = You.Hit_points + You.Skill_points + Opponent.Health

Thank you!

At least one of Opponent.Health , You.Hit_points , and You.Skill_points is a string, and at least of them is a number(an int). You're trying to add together strings and numbers. If you intend for all of those values to be numbers, you need to figure out which one isn't and change that. You could cast all of the values to int but that's a short-term solution, this is a problem that will keep coming up if you don't fix it.

All the information you need is in the error: unsupported operand type(s) for +: 'int' and 'str'

Hit_points is probably an int. Convert it to a string:

 str(You.Hit_points)

Edit:

Wait, no. Misread, Nolen Royalty is correct. This would probably suffice:

Opponent.Health=int(You.Hit_points)+int(You.Skill_points)+int(Opponent.Health)

But I would follow Nolen's recommendations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM