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 trialEthan Koren
6,277 Pointseven.py
how do i use the even_odd function?
import random
start = 5
def even_odd(num):
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
return not num % 2
while start = True
num = random.randint(1, 99)
even_odd(num):
if
1 Answer
dojewqveaq
11,393 PointsHi Ethan!
I particularly find this task very confusing, but I'll try to explain as best as I can. You don't need to explicitly say start = True. Here is how it works:
if something: # This already evaluates to True
But this is not crucial, just a little tip ;)
if num % 2 == 0 # This is used to tell if a number is even or odd, if it evaluates to True, than it's even, if evaluates to False,
# it's even.
if not num % 2 == 0 # This is the same thing, but since we used the not, it inverts the result.
# So the function even_odd(num) returns True if num is odd, because it's inverted by the not
Ok, now that we got that out of the way, here is one way to solve this task
import random
def even_odd(num):
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
return not num % 2
start = 5
while start:
num = random.randint(0, 99) # Here you generate a random number between 0 and 99
if even_odd(num): # This evaluates if the function even_odd() passing the argument num is True, if it is, it will execute the following code block
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1 # This statement will decrease the start by 1
I hope this helped! =)