TypeError in Python Code

Hi everyone,

Just today I was testing my text-based fighting game (Super Smash Dogs) and I was suddenly interrupted by a TypeError in my code:

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

I know this error means that I tried to concatenate an integer and a string together but the traceback tells me that the error occurred at line 2016 in main.py, in the play() function, which looks like this:

turn_average = sum(turn_history) / len(turn_history)

Just to note, turn_history is a 1 dimension list made up entirely of integers, which is why I don’t understand what the issue is with my code. If anyone knows why this is happening or how I can solve the issue, I would greatly appreciate it.

Thanks in advance,

Replit link: https://replit.com/@CCodes/Super-Smash-Dogs-135

It looks like that error comes from inside the sum function because of some inconsistency within turn_history. Try adding print(turn_history) before line 2016 to examine and verify the types in turn_history, since you might have some code which implicitly converts int to str, or some other obscure bug.

1 Like

I added the print(turn_history) line but now I have an issue before the code even reaches that point. When I ran the single player mode, a few turns into the battle it gave me this error:

Screenshot 2024-07-03 8.47.42 AM

I’m not sure why it could be happening. Is there any way you could assist me with this?

The error in line 2016 maybe because one element in turn history may be an str. And the error in line 1816 is because an escape code was returned with the input, which cannot be returned as an integer. The escape code \x1b[B is the down arrow key.

Try to clean the entry before converting (line 1816), add a while to remove non-numeric char (maybe the input is capturing non-numeric chars)

def p1_turn():
    slowprint("P1 Moves", 'blue')
    for i, move in enumerate(P1MOVES):
        slowprint(f"{i + 1} - " + move['name'], 'blue')
        slowprint(f"Move effect: {move['effect']}")
    slowprint("Enter your move number.")
    while True:
        move_num_input = input(": ")
        # Like I said remove the non-numeric chars that might exist
        move_num_input = re.sub(r'\D', '', move_num_input)
        if move_num_input.isdigit():
            move_num = int(move_num_input) - 1
            if 0 <= move_num < len(P1MOVES):
                slowprint("Invalid move number. Please enter a valid number.")
            slowprint("Invalid input. Please enter a number.")
    move = P1MOVES[move_num]

Hey guys, thanks for all your support. Sorry for the late response - I was on vacation, but somehow, when I tried @WindLother’s solution to the line 1816 issue, both issues simultaneously solved themselves somehow? Not sure if the issue I had in the first place somehow only happens in a certain matchup? Anyway, thanks again to everyone who replied to this thread.

Well, the error happens because the user’s input is not being correctly converted to an integer, which resuls in a ValueError (invalid literal for int).
This happens when the input() captures non-numeric characters, which is why I said to you add a check and clean the input before attempting to convert it to an integer

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.