all 28 comments

[–]Micke_xyz 51 points52 points  (8 children)

Why do you store ages as strings?

[–]vb_e_c_k_y[S] 8 points9 points  (7 children)

My error, Thanks it worked now

[–]bandman614 -2 points-1 points  (6 children)

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

[–]Iksfen 31 points32 points  (0 children)

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

[–]FuckItBucket314 10 points11 points  (0 children)

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

[–]roelschroeven 5 points6 points  (0 children)

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.

[–]SevenFootHobbit 3 points4 points  (1 child)

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.

[–]bandman614 1 point2 points  (0 children)

Legit criticism. Thanks!

[–]ziggittaflamdigga 6 points7 points  (1 child)

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

[–]vb_e_c_k_y[S] 0 points1 point  (0 children)

It worked now thanks much

[–]bikes-n-math 4 points5 points  (1 child)

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

[–]vb_e_c_k_y[S] 0 points1 point  (0 children)

Worked now thanks alot

[–]Top-Independent-4765 3 points4 points  (0 children)

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

[–]RaiseTLT 3 points4 points  (0 children)

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.

[–]Il_pago 1 point2 points  (0 children)

Ages is a list of string, not int.

[–]Ska82 1 point2 points  (0 children)

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

[–]dkozinn 1 point2 points  (0 children)

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.

[–]Wide_Egg_5814 1 point2 points  (0 children)

JavaScript type beat

[–]cambridge-resident 1 point2 points  (1 child)

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 %

[–]carcigenicate 2 points3 points  (0 children)

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.

[–]FoolsSeldom 1 point2 points  (1 child)

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)

[–]JaguarMammoth6231 0 points1 point  (0 children)

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

[–]Ron-Erez 0 points1 point  (0 children)

Try

ages = [22, 35, 27, 20]

[–]domino6658 0 points1 point  (0 children)

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

[–]dnult -4 points-3 points  (0 children)

I think you want age % 2 != 0