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 trialStefan Vaziri
17,453 PointsHELP
I think I'm close but I'm not sure what's missing...
def even_odd(num):
guess = int(input("Guess a number?"))
if guess == (num%2 == 0):
print("True")
else:
print("False")
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe 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
Magali Doucet
2,330 PointsI 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
Treehouse Moderator 68,457 PointsThe "%" 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.
Magali Doucet
2,330 PointsThank you Chris!
Stefan Vaziri
17,453 PointsStefan Vaziri
17,453 PointsGot it. Thanks so much Chris!