Swapping variable places inside if throws an error

I think you algorithm should be rethought.

Basically what you’re doing is

for i in [0..N]:
    for k in [i..N]:
        my_string_1 = data[i][0]
        my_string_2 = data[k][0]
        if my_string_1 == my_string_2:
            data[i] = 0
            data[k] = 0

The issue here is that at some point, the condition is verified and you assign 0 to data[k], but then in another i-loop iteration, for the same k value you read this value back and treat it as if it was some of those 'A268981' strings, which fails.

Example:

i = 3
k = 7
data[k] = 0

then later

i = 5
k = 7
data[k][0]  <-- error

I didn’t run your code but I suppose this may or may not happen depending on the other condition if(tyre1 - tyre2 < TWENTY_MPH and tyre1 - tyre2 > SIXTY_MPH):, which is why you see it or not depending on that condition.

Also, I don’t know what those constants are, but I don’t see how the same value could be both < 20 and > 60. You should post those values as well.

Note: when dealing with strings, data_list[i][0] is equivalent to and more readable in my opinion than data_list[i][:1].

Leave a Comment