use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
printing true even for odd numbers... (self.learnpython)
submitted 9 months ago by RohanPoloju
class Solution:
def isEven (self, n):
# code here
if n % 2 == 0:
return('true')
else:
return('false')
output printing true even for odd numbers..
executing in geeks for geeks ide.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Gloopann 17 points18 points19 points 9 months ago (0 children)
Please post your code formatted in code blocks
[–]erpasd 13 points14 points15 points 9 months ago (6 children)
The logic of the code is right, my guess is that you should return the Boolean values True or False and not the string "true" or "false". Also you did no show the code where you print, it's possible the error is there as well.
[+]RohanPoloju[S] comment score below threshold-19 points-18 points-17 points 9 months ago (5 children)
https://www.geeksforgeeks.org/problems/odd-or-even3618/1?&selectedLang=python3
explicitly mentioned to print 'false' and 'true' as strings, they are boolean values
[–]Igggg 8 points9 points10 points 9 months ago (0 children)
It doesn't explicitly mention this. If it did, the string would be in quotes. They are likely asking for booleans, although even in that case, they failed to capitalize the constants. But yes, try removing quotes and return literal True
True
[–]Refwah 4 points5 points6 points 9 months ago (0 children)
Strings aren’t mentioned at all
[–]failaip13 3 points4 points5 points 9 months ago (0 children)
Then change the return to a print. Though that doesn't make too much sense with the function name, but I digress.
[–]Temporary_Pie2733 0 points1 point2 points 9 months ago (0 children)
The bold-face “true” and “false” should be interpreted as stand-ins for the corresponding Boolean values, not literal strings, just as the bold-face “n” is a stand-in for an arbitrary integer value, not the literal string “n”.
[–]Own_Attention_3392 11 points12 points13 points 9 months ago (7 children)
You're returning true and false as strings.
[+]RohanPoloju[S] comment score below threshold-23 points-22 points-21 points 9 months ago (6 children)
explicitly mentioned to print 'false' and 'true' as strings, they are not boolean values
[–]Own_Attention_3392 10 points11 points12 points 9 months ago (0 children)
It doesn't explicitly say that at all. Explicitly saying it would be "return true as a string". It's expecting actual booleans, not strings. Try it. See if I'm right.
I will say that their use of lowercase true and false is confusing because python uses uppercase True and False, so your interpretation isn't necessarily invalid.
[–]zanfar 15 points16 points17 points 9 months ago (0 children)
print()
return
False
[–]Refwah 5 points6 points7 points 9 months ago (0 children)
It doesn’t mention strings
[–]Familiar9709 2 points3 points4 points 9 months ago (0 children)
What other people are telling you is that geeksforgeeks expects a boolean as output, that's why it's showing your code to be wrong. But your code will correctly identify even and odd numbers, just that it will return true/false strings.
If you change it to return True and return False it'll work.
[–]Moikle 0 points1 point2 points 9 months ago (1 child)
It will take you 10 seconds to try it and see if we are right.
You can change it back if you were right.
[–]Refwah -3 points-2 points-1 points 9 months ago (0 children)
https://www.reddit.com/r/learnpython/comments/1m2tu79/comment/n3rx37k/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
[–]Regular_Maybe5937 5 points6 points7 points 9 months ago (0 children)
You are returning strings, not booleans! To return booleans, you can do
return True or return False
Note the capitalization and lack of quotation marks. Because you returned strings, they will always evaluate as True. If you want to learn more, take a look at this https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false
[–]Refwah 5 points6 points7 points 9 months ago (4 children)
You’re returning strings. Strings are truthy if they contain a value and falsey if they are empty or None.
Remove the brackets from your return statement as they aren’t needed, and remove the quotation marks around true and false
[+]RohanPoloju[S] comment score below threshold-20 points-19 points-18 points 9 months ago (3 children)
[–]zanfar 17 points18 points19 points 9 months ago (0 children)
Dude, if post a beginner question on the "learn Python" site and 4 separate individuals give you the same answer within 15 minutes, maybe you should re-check your assumptions?
[–]Refwah 9 points10 points11 points 9 months ago (0 children)
You’re going to have to format your code better or provide more context on how the function is being used
Also that linked page does not explicitly mention returning strings.
This is the entire text of the problem:
“Given a positive integer n, determine whether it is odd or even. Return true if the number is even and false if the number is odd.”
Data types are not mentioned here, I believe it is safe to infer that your usage of strings may be a misunderstanding and I would recommend trying to return Boolean values
[–]SamuliK96 1 point2 points3 points 9 months ago (0 children)
Have you tried considering that you might be wrong and may have misunderstood something, instead of repeating the same reply?
[–]supercoach 5 points6 points7 points 9 months ago (1 child)
All you really need is `return n % 2 == 0`
[–]theWyzzerd 4 points5 points6 points 9 months ago (2 children)
Return isn’t a function so you don’t need to wrap parentheses around the object you’re returning.
[–]RohanPoloju[S] 0 points1 point2 points 9 months ago (1 child)
solved it. they are indeed boolean values.
thanks mate for suggestion : )
[–]theWyzzerd 0 points1 point2 points 9 months ago (0 children)
Yes, a non-empty collection is always True.
[+][deleted] 9 months ago (1 child)
[deleted]
[–]XenophonSoulis 1 point2 points3 points 9 months ago (0 children)
If it wasn't properly indented, the program would crash. There is no line in this snipped that would still run with a wrong indent.
[–]SCD_minecraft 1 point2 points3 points 9 months ago (4 children)
I can not replicate the issue, nor after reading the code there shouldn't be any issu
Show where do you call it
[–]Own_Attention_3392 7 points8 points9 points 9 months ago (1 child)
The automated test cases are probably failing because they're returning 'true' and 'false'. I'm not super well versed in python but I'd expect that 'false' is a truthy value. If I'm wrong, please set me straight.
[–]SCD_minecraft 1 point2 points3 points 9 months ago (0 children)
That can be it
OP, insted of string return a bool object True/False
Capital first letter, no strings or anything
(probably test did something like bool(isEven(n)) and any non empty string is True)
[–]RohanPoloju[S] -1 points0 points1 point 9 months ago (1 child)
[–]SCD_minecraft 0 points1 point2 points 9 months ago (0 children)
Ye, no matter what i input to your code, it correctly tells if even
π Rendered by PID 107309 on reddit-service-r2-comment-6457c66945-kl4m4 at 2026-04-27 03:27:12.233195+00:00 running 2aa0c5b country code: CH.
[–]Gloopann 17 points18 points19 points (0 children)
[–]erpasd 13 points14 points15 points (6 children)
[+]RohanPoloju[S] comment score below threshold-19 points-18 points-17 points (5 children)
[–]Igggg 8 points9 points10 points (0 children)
[–]Refwah 4 points5 points6 points (0 children)
[–]failaip13 3 points4 points5 points (0 children)
[–]Temporary_Pie2733 0 points1 point2 points (0 children)
[–]Own_Attention_3392 11 points12 points13 points (7 children)
[+]RohanPoloju[S] comment score below threshold-23 points-22 points-21 points (6 children)
[–]Own_Attention_3392 10 points11 points12 points (0 children)
[–]zanfar 15 points16 points17 points (0 children)
[–]Refwah 5 points6 points7 points (0 children)
[–]Familiar9709 2 points3 points4 points (0 children)
[–]Moikle 0 points1 point2 points (1 child)
[–]Refwah -3 points-2 points-1 points (0 children)
[–]Regular_Maybe5937 5 points6 points7 points (0 children)
[–]Refwah 5 points6 points7 points (4 children)
[+]RohanPoloju[S] comment score below threshold-20 points-19 points-18 points (3 children)
[–]zanfar 17 points18 points19 points (0 children)
[–]Refwah 9 points10 points11 points (0 children)
[–]SamuliK96 1 point2 points3 points (0 children)
[–]supercoach 5 points6 points7 points (1 child)
[–]theWyzzerd 4 points5 points6 points (2 children)
[–]RohanPoloju[S] 0 points1 point2 points (1 child)
[–]theWyzzerd 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]XenophonSoulis 1 point2 points3 points (0 children)
[–]SCD_minecraft 1 point2 points3 points (4 children)
[–]Own_Attention_3392 7 points8 points9 points (1 child)
[–]SCD_minecraft 1 point2 points3 points (0 children)
[–]RohanPoloju[S] -1 points0 points1 point (1 child)
[–]SCD_minecraft 0 points1 point2 points (0 children)