TypeError: not all arguments converted during string formatting

Sweet, two problems here

First, your jdoodle assumes you are getting in int array, you are not. You are getting a string of numbers. So the first thing you need to do is take that string of numbers and split it into an array. Then you need to convert each item in the array to an int.

Here is what I came up with

def iq_test(input):
    a = input.split(" ")
    ct = 0
    cf = 0

    for i in a:
        if int(i) % 2 ==0:
            ct = ct + 1
        else:
            cf = cf + 1
    if ct > cf:
        for i in a:
            if int(i) % 2 !=0:
                y  = a.index(i) + 1
            
        return y
    else:
        for i in a:
            if int(i) % 2 == 0:
                y  = a.index(i) + 1
            
        return y

Note, how I split the input into an array as the first step, then everywhere you referenced i % 2, I changed it to int(i) % 2 which permits it to do a modulus. Now this can fail, but the kata doesn’t, if the following input was provided, this would still blow up 3 5 8 9 N, as ‘N’ can’t be converted to an int and we’d have to add isinstance(i, int) before running int(i) % 2 == 0, which we talked about at

But as I said, the tests the kata is using will pass with the above code. :slight_smile:

1 Like