Python Puzzle #1 (codewars - 8kyu)
name:
even or odd
description:
Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
my solution:
def even_or_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
explication:
We are asked to create a function that takes an int as argument. Basically it should return "even" if the number entered is even (divisible by 2) or "odd" if not.
How do we check if the number is divisible by 2? Well we can use the modulo operator (%) that return the rest of a division. Implement it in an if statement to verify if the number entered divided by 2 gives a rest equal to 0. If so the number is even. If not it is odd.
Subscribe to my newsletter
Read articles from cybermetaltech directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by