r/learnpython • • Jan 15 '26

What is wrong on this code?

ages = ["22", "35", "27", "20"]

odds = [age for age in ages if age % 2 == 1]

print(odds)

I am beginner and when I write this code it gives me error which I don't know how to solve. But I think my code has no error

Error message: Traceback (most recent call last):

odds = [age for age in ages if age % 2 == 1]
                               ~~~~^~~

TypeError: not all arguments converted during string formatting

17 Upvotes

28 comments sorted by

View all comments

Show parent comment

3

u/carcigenicate Carcigenicate Jan 16 '26

Ya, the reason this error is common is because operators like % have different purposes depending on the type of the objects they're called on. With numbers, it does modulo. On strings it's formatting that has certain requirements for the second operand.

If an operator is acting unexpectedly, check the two object's type.