Write a function that determines whether a number is even or odd. The core logic is simple, but this challenge is about writing it safely and predictably.
Your Task
Create a function:
def even_or_odd(n: int) -> str:
…
It must return:
"Even"ifnis even"Odd"ifnis odd
Rules
nmust be an integer.- If
nis not an integer, raiseTypeError. - Treat booleans as invalid input (even though
boolis technically a subclass ofintin Python).
Examples
even_or_odd(4)→"Even"even_or_odd(7)→"Odd"even_or_odd(0)→"Even"even_or_odd(-3)→"Odd"even_or_odd(2.0)→ raisesTypeErroreven_or_odd(True)→ raisesTypeError
Hints
- Use the modulo operator:
% - Input validation should happen before doing math.
(Click on “Python Solution Code” or Python Test Code” to show the code)


The test code is overwhelming at first. Just read the code and take your time. Better to add this code to your solution code and play with the values for testing if these are odd or even.
Nice work!
You’ve completed this challenge. The best way to improve your Python skills is to keep practicing while the concept is fresh.
This challenge keeps your progression looking like this:
- Reverse a string safely
- Count words in a sentence
- Find the largest number in a list
- Count character frequency
- Filter even numbers
- Even or odd (logic & validation focus)
Nice, natural flow.
Encouring you to take some extra time to check the test code. I’ll be using this more in the coming challenges.
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
👉 Next Challenge → Sum Numbers in a List
Want more practical Python challenges?
Subscribe to the Solve With Python newsletter and get new problems delivered to your inbox.