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

Stefan Vaziri
Stefan Vaziri
17,453 Points

HELP

I think I'm close but I'm not sure what's missing...

even.py
def even_odd(num):
  guess = int(input("Guess a number?"))
  if guess == (num%2 == 0):
    print("True")
  else:
    print("False")

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The challenge asks Write a function named even_odd that takes a single argument, a number. Return True if the number is even, or False if the number is odd.

You don't need to ask for input. The function argument is how the input is passed to the function.

The built in values True and False should be returned instead of strings.

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

The if conditional can be used directly as the return value:

def even_odd(num):
    return num % 2 == 0
Stefan Vaziri
Stefan Vaziri
17,453 Points

Got it. Thanks so much Chris!

Magali Doucet
Magali Doucet
2,330 Points

I dońt understand why the num divise by 2 should equal 0. Why dońt we just ask the function ; if you can split by 2 the number and it stay a float. Return true, else return false

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

The "%" symbol means modulo division. Which is a fast way to get the remainder from an integer division. In Binary arithmetic, "% 2" result can be quickly found my examining the least significant bit. 1 is odd 0 is even.

Other ways can determine odd vs. even such as seeing if float is created by odd number divided by two. But given the speed of modulo math it has become a well recognized idiom.