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 trialYusuf Gillani
Python Web Development Techdegree Student 3,643 PointsCan someone give me a hint please. I'm a little stuck.
How do I do the function so if it's even it's True and not It's False.
import random
start = 5
def even_odd(num):
# If % 2 is 0, the number is even.
if num % 2 = 0:
else:
# Since 0 is falsey, we have to invert it with not.
return not num % 2
while True:
num = random.ranint(1,99)
if even_odd = 0:
print" {} is even".format(num)
else:
print(" {} is odd".format(num)
2 Answers
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsYou should not change anything in the even_odd function.
if even_odd(number): # even_odd(1) returns False / even_odd(2) returns True
I hope this helps :-)
Unsubscribed User
6,415 PointsHi. Since you're asking for a hint, I'll try to nudge you in the right direction with a couple observations here:
All of your conditions here are using the single equals sign. However, that is used to assign values to variables as in
v = 3
. To compare a value to another, you'll need the double equals sign as inif x % 2 == 0:
.Nothing is actually taking place as a result of your
if...: else:...
structure inside theeven_odd(num)
function.The method for getting a random integer using the random module is
random.randint()
as opposed torandom.ranint()
.You're not calling your
even_odd
function on line 16 since there are no parentheses, and be sure to call it with an argument to be passed to thenum
parameter you define for it.You don't define a
break
statement inside yourwhile True:
loop so you're ending up with an infinite loop.
Hope this helps!