Q:

How to fix 'TypeError: not all arguments converted during string formatting' in Python

I'm trying to make a code to get two keys from a master key. The key must be a string. For the first, you have to take the even number and for the second, the odds numbers. E.g.: Master key = 18923. First Key = 82, second Key = 193.
I am new to python.
masterKey = '18293'
firstKey = ''
secondKey = ''

masterKeyList = list(masterKey)
firstKeyList = list(firstKey)
secondKeyList = list(secondKey)

for digit in masterKeyList:
    **if digit % 2 == 0:** *<--- here is the error*
        firstKeyList.append(digit)
    else:
        secondKeyList.append(digit)

 *if digit % 2 == 0: <--- Error message
    TypeError: not all arguments converted during string formatting*

I want to know why this happens, and a solution. Thank you!

A:

digit is a string. Just typecast it using int():
if not int(digit) % 2:

The reason your error is occurring is because the modulus operator is also a string formatting operator, but you haven't provided the necessary arguments to string-format '2'
This for-loop will work:
for digit in masterKeyList:
    if not int(digit) % 2:
        firstKeyList.append(digit)
    else:
        secondKeyList.append(digit)

