r/PythonLearning • • 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 ):")

0 Upvotes

12 comments sorted by

•

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.

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

u/Several_Goal4568 Jun 11 '26

Thank you for the feedback. Will add quit option ,

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

u/Several_Goal4568 Jun 11 '26

Thank you for the feedback, will use this one line function.

2

u/sleepbot63 Jun 11 '26

little improvements

3

u/cole36912 Jun 11 '26

Please use a code block for posting code. Also, what is the d=2 for?

-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

u/Several_Goal4568 Jun 11 '26

Learned a new way from you , thank you for the feedback.

2

u/[deleted] Jun 11 '26

[removed] — view removed comment

1

u/Several_Goal4568 Jun 11 '26

To prevent system crash used try except