Search code examples
pythonpython-3.xxor

not all arguments converted during string formatting python


I am writing a script which prints the list items that are divisable by 3 XOR the last character ==4 but I get an error stating: TypeError: not all arguments converted during string formatting I am new to Python and may have missed something obvious. Code below:

lijst = ["124576", "795834", "890432", "907251"]    
for j in lijst:
if j[-1]==4 ^ j%3 > 0  :
    print(j)

Solution

  • First of all you need to change 4 to '4' because your items are string and convert the j to int in j%3 also you need to parenthesis your comparison expression because the precedence of ^ is higher than == and it will raise a TypeError also if you want to preserve your result you can use a list comprehension :

    >>> [j for j in lijst if (j[-1]=='4') ^ (int(j)%3 > 0)]
    ['124576', '795834', '890432']
    

    If you just want to print the result you can use a usual loop with print function.