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

Why code 1 or code 2 is not working i can't get the logic

1-) def even_odd(num): a= int if num % 2 == a: return True else: return False

2-) def even_odd(num):

if num % 2 == int : return True else: return False

even.py
def even_odd(num):
    a= int
if num % 2 == a:
    return True
else:
    return False

def even_odd(num):

if num % 2 == int :
    return True
else:
    return False

2 Answers

Hi there,

You've correctly identified that using the mod operator % is the way forward here. You've also defined a method called even_odd, as required. This receives a number as a parameter.

What you want to do is to see if the number passed in is even. For that to be the case, using the mod operator and the number 2 will evaluate to zero if the parameter passed in is even, or not zero (it'll be 1, but hey) if the parameter is odd.

Let's make the method skeleton:

def even_odd(num):
  # return True if even

So, we want to use the mod operator on num. This would look like: num % 2. We want to know if that equals zero. We can compare this to zero using double-equals, ==. This gives us the ability to make the comparison with:

num % 2 == 0

You've thought about using an if statement to return the correct value. As it happens, you don't need to. The above expression will evaluate to either True or False. We don't need any more code. There no point in this:

var = True
if var == True:
  return True
else:
  return False

That's a lot of pointless code! So in our method, just return the result of the comparison:

def even_odd(num):
  return num % 2 == 0

i hope that makes sense.

Steve.

that totally made sense thnx...

No problem - happy to help! :+1: