Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,082 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,082 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.