Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Number Game App Even or Odd

victor E
victor E
19,145 Points

I'm having a hard time understanding the instructions.

So how does this one work?

even.py

2 Answers

Hi Victor!

This one can get tricky, so let me walk you through it.

So we know that an even number is always divisible by 2.

What we need to do is to check whether the number that the user gave is divisible by 2. If it is, it is even. If it is not, it is odd.

It asks us to define a function even_odd and give it one argument. I will call it num.

def even_odd(num):

No look at the description I gave earlier. I used the key word "if" a lot. We will need to use an if statement.

First we check whether num is divisible or not. If it is, return true. We can check this using the modulo or % sign.

if num % 2 == 0:
  return True

If you don't understand the modulo sign, I suggest going back to the video a few times.

If it is not divisible by 2, then return False. We do not need to type another condition. We can just use else because the number has to be either even or odd. So if it is not even, it has to be odd.

else:
   return False

Here is all the code:

def even_odd(num):
    if num % 2 == 0:
        return True
    else:
        return False

I hope this helps!

If you have any more questions please let me know.

Thanks! :)

victor E
victor E
19,145 Points

Thank you very much for your help! I dont recall the video going over the % sign but ill have to look into it.

Much appreciated!