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 trialRichard Farr
4,857 Pointsimport random not working and rest not too?
It says
import random and the function does not work but I can't figure out why.
any suggestions?
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>0):
randnum = random.randint(1, 99)
oddeven = even_odd(randnum)
if oddeven == 0
print ("{} is even".format(randnum))
else:
print ("{} is odd".format(randnum))
start = start -1
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyour function returns a boolean, but in the while loop you are comparing it to 0. change 0 to true, and also you are missing the colon : after the 0 in the if statement.
Chris Freeman
Treehouse Moderator 68,441 PointsYou're very close. There is a missing colon:
if oddeven == 0 # <-- missing colon
This causes a syntax error which throws off all the previous task checks. The random
import is ok.
The other error is a reversed polarity. even_odd
returns True
if it's even. So the you'll need to use if oddeven == 1:
or, better yet, use the "truthiness" and simply code: if oddeven:
Richard Farr
4,857 Pointsthank you. got it.
Richard Farr
4,857 PointsRichard Farr
4,857 Pointsthank you