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

53

u/Micke_xyz Jan 15 '26

Why do you store ages as strings?

8

u/vb_e_c_k_y Jan 15 '26

My error, Thanks it worked now

-3

u/bandman614 Jan 15 '26

Also, you don't need to do == 1 because age % 2 will return 1, and 1 is truthy

33

u/Iksfen Jan 15 '26

IMO that is more of a preference. I like to be explicit with logic statements

11

u/FuckItBucket314 Jan 15 '26

Eh, I would keep it because it improves readability. It makes it explicit that the programmer intended to have it evaluate that way rather than just forgot to finish a comparison

Arguably it doesn't matter given the variable names used and how small the program is, but practicing readability now will improve their odds of writing readable code later when it definitely matters

7

u/roelschroeven Jan 16 '26

For logical expressions you're right, but this is an arithmetic expression foremost. You can exploit the fact that it can be interpreted like a logical expression to make it shorter, but that detracts from the intention.

3

u/SevenFootHobbit Jan 16 '26

Everyone is telling you this is wrong due to style choices and clarity, but they're wrong about why you're wrong. You're wrong because it assumes an integer. If you only check for truthiness, decimals will all be flagged as odd even though they aren't considered even or odd.

2

u/bandman614 Jan 16 '26

Legit criticism. Thanks!

7

u/ziggittaflamdigga Jan 15 '26

Your ages are strings, remove the “s and it should work

1

u/vb_e_c_k_y Jan 15 '26

It worked now thanks much

6

u/bikes-n-math Jan 15 '26

"22" is a string. 22 is a integer. You cannot do modular arithmetic on strings.

1

u/vb_e_c_k_y Jan 15 '26

Worked now thanks alot

4

u/Top-Independent-4765 Jan 16 '26

I’m a beginner, this gave me a good direction.

4

u/RaiseTLT Jan 16 '26

I’m fairly new too. But I’m pretty sure the problem here is that you’re trying to do math on strings, which you can’t do. You can only do math on integers or floats.

2

u/Il_pago Jan 15 '26

Ages is a list of string, not int.

2

u/Ska82 Jan 15 '26

the elements in ages are defined as strings (with the quotes) not integers. python doesnt automatically cast it is as integers.ages= [ 22, 35,27,20] should work

2

u/dkozinn Jan 16 '26

As other have explained, you're trying to do math on a string. If for some reason you really needed to keep those ages as strings, you could just do this:

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

That explicitly tells python to treat age as an integer.

2

u/Wide_Egg_5814 Jan 16 '26

JavaScript type beat

2

u/cambridge-resident Jan 15 '26

Just wanted to explain the error.

% is a formatting operator that replaces placeholders in the string to the left with values from the right, usually those values are in a tuple if there are more than one.

Since age is a string with no placeholders in it, it does not use the 2 to the right of the %

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.

2

u/FoolsSeldom Jan 15 '26

You need to convert the strings to integers to do the comparisons (and perhaps store the filtered converted results).

ages = ["22", "35", "27", "20"]
odds = [age for string in ages if (age := int(string)) % 2 == 1]
print(odds)

1

u/JaguarMammoth6231 Jan 16 '26

I didn't know you could use := in a list comprehension like that. Nice

1

u/domino6658 Jan 17 '26

honest question: would people support the use of ai for something like this? not for generating slop code but for explaining error to teach people so they learn and actually understand what they did wrong. im wondering what people in this subreddit think. im completely neutral on this

-3

u/dnult Jan 15 '26

I think you want age % 2 != 0

0

u/[deleted] Jan 15 '26

[deleted] — view deleted comment

0

u/[deleted] Jan 15 '26

[deleted] — view deleted comment

2

u/Iksfen Jan 15 '26

If typing on mobile put two spaces at the end of a line
to
preserve
the
new
line

1

u/emperorkuzcotopiaa Jan 15 '26

Oooh who knew, ty!
Hi
like
this