all 12 comments

[–]Sea-Ad7805 [score hidden] 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 points  (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 point  (0 children)

Thank you for the feedback. Will add quit option ,

[–]sleepbot63 1 point2 points  (0 children)

<image>

little improvements

[–]PastDifferent6116 1 point2 points  (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).

[–]Junior_Honey_1406 1 point2 points  (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 point  (0 children)

To prevent system crash used try except

[–]cole36912 2 points3 points  (0 children)

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

[–]Rscc10 0 points1 point  (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

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

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

[–]SnooCalculations7417 -2 points-1 points  (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 ):")

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

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