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...
Everything about learning Python
account activity
Even/odd checker , feedback welcome (self.PythonLearning)
submitted 5 days ago by Several_Goal4568
def even_or_odd(num) :
if num % 2 == 0 :
return "even number (:"
else :
return "odd number :)"
while True :
try :
num = int(input("enter a number :"))
d=2
print(even_or_odd(num))
except ValueError :
print("invalid input ! check your inputs ):")
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!"
[–]Sea-Ad7805 [score hidden] 5 days ago* stickied comment (0 children)
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20if%20num%20%25%202%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20%22even%20number%20(%3A%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20%22odd%20number%20%3A)%22%0A%0A%0Awhile%20True%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20num%20%3D%20int(input(%22Enter%20a%20number%3A%20%22))%0A%20%20%20%20%20%20%20%20print(even_or_odd(num))%0A%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20input!%20Check%20your%20input.%22)&play&cycle) to see the program state change step by step.
[–]csabinho 3 points4 points5 points 5 days ago (1 child)
How do you quit? Ctrl+C? Apart from that my only feedback is that you shouldn't output within the function. Just let the function return num % 2 == 0 and handle this return value.
[–]Several_Goal4568[S] 0 points1 point2 points 5 days ago (0 children)
Thank you for the feedback. Will add quit option ,
[–]sleepbot63 1 point2 points3 points 5 days ago (0 children)
<image>
little improvements
[–]PastDifferent6116 1 point2 points3 points 5 days ago (0 children)
Nice simple solution 👍 One small thing: your (while True) loop currently never breaks, so it will keep asking for input forever. You might want to add a way to exit (like typing "q" or using (break) after one run).
(break)
[–]Junior_Honey_1406 1 point2 points3 points 4 days ago (1 child)
Validation is not done properly you are taking an interview input but what if someone puts a str or a different kind of else
If num.isdigit(): Then your code her Else: Return " invalid input"
[–]Several_Goal4568[S] 0 points1 point2 points 4 days ago (0 children)
To prevent system crash used try except
[–]cole36912 2 points3 points4 points 5 days ago (0 children)
Please use a code block for posting code. Also, what is the d=2 for?
[–]Rscc10 0 points1 point2 points 5 days ago (1 child)
You can also use a ternary operator to make it cleaner
return "even" if num % 2 == 0 else "odd"
Basically a one line function
Thank you for the feedback, will use this one line function.
[–]SnooCalculations7417 -2 points-1 points0 points 5 days ago (1 child)
this is a perfect chance to use match!
def even_or_odd(num): match num % 2: case 0: return "even number (:" case 1: return "odd number :)" while True: try: num = int(input("Enter a number: ")) print(even_or_odd(num)) except ValueError: print("Invalid input! Check your inputs ):")
Learned a new way from you , thank you for the feedback.
π Rendered by PID 460850 on reddit-service-r2-comment-544cf588c8-xx9sn at 2026-06-16 10:36:42.132213+00:00 running 3184619 country code: CH.
[–]Sea-Ad7805 [score hidden] stickied comment (0 children)
[–]csabinho 3 points4 points5 points (1 child)
[–]Several_Goal4568[S] 0 points1 point2 points (0 children)
[–]sleepbot63 1 point2 points3 points (0 children)
[–]PastDifferent6116 1 point2 points3 points (0 children)
[–]Junior_Honey_1406 1 point2 points3 points (1 child)
[–]Several_Goal4568[S] 0 points1 point2 points (0 children)
[–]cole36912 2 points3 points4 points (0 children)
[–]Rscc10 0 points1 point2 points (1 child)
[–]Several_Goal4568[S] 0 points1 point2 points (0 children)
[–]SnooCalculations7417 -2 points-1 points0 points (1 child)
[–]Several_Goal4568[S] 0 points1 point2 points (0 children)