r/PythonLearning • u/Several_Goal4568 • Jun 11 '26
Even/odd checker , feedback welcome
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 ):")
5
u/csabinho Jun 11 '26
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.
1
1
u/Rscc10 Jun 11 '26
You can also use a ternary operator to make it cleaner
return "even" if num % 2 == 0 else "odd"
Basically a one line function
1
2
3
-1
u/SnooCalculations7417 Jun 11 '26
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 ):")1
2

•
u/Sea-Ad7805 Jun 11 '26 edited Jun 11 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.