Challenge 6: Even or Odd (With Input Validation)

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" if n is even
  • "Odd" if n is odd

Rules

  1. n must be an integer.
  2. If n is not an integer, raise TypeError.
  3. Treat booleans as invalid input (even though bool is technically a subclass of int in 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) → raises TypeError
  • even_or_odd(True) → raises TypeError

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)

Python Challenge 6 Even or Odd (With Input Validation)
Python Challenge 6 Even or Odd (With Input Validation)

Python Challenge 6 Test Even or Odd (With Input Validation)

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:

  1. Reverse a string safely
  2. Count words in a sentence
  3. Find the largest number in a list
  4. Count character frequency
  5. Filter even numbers
  6. 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.