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 trialJustin Noor
3,692 PointsCan anyone give me some hints as to why my even_odd loop is not working?
I've been breaking my head for a few on this one. My loop generates the correct number of prints but it's not passing the quiz. I suspect it has something to do with the 'return not num % 2' line. Can anyone give me any hints as to why my code is not passing the quiz?
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:
some_num = random.randint(0, 99)
even_odd(some_num)
if even_odd(some_num):
print("{} is even.".format(some_num))
else:
print("{} is odd.".format(some_num))
start -= 1
4 Answers
Jennifer Nordell
Treehouse TeacherJustin Noor First of all, you're doing great. Now you may or may not have noticed this... but challenges are super picky. First, you have a line in there that's not needed:
even_odd(some_num)
But just removing this, won't fix the problem. It still fails the challenge. Why? Because you have periods/full stops on the end of your strings. Seriously, that's the big problem! Make these tiny adjustments and your code passes
Steven Parker
231,248 PointsYou're going to love this - it apparently doesn't like the periods you added the the print strings!
Also, though it doesn't make the challenge fail, you have a couple other issues:
- you have a line with this expression:
even_odd(some_num)
- it doesn't do anything by itself - the challenge asked for random numbers chosen between 1 and 99, but you choose between 0 and 99.
Happy codling!
Jennifer Nordell
Treehouse TeacherSteven Parker that's crazy right? Odd that the random number thing doesn't make it fail, but the period... too much to handle!
Steven Parker
231,248 PointsWell, there's a 1 in 20 chance that it might make it fail.
Actually I just tried it, they don't care what the numbers are. I picked from 100 to 199.
Damien Watson
27,419 PointsHey Justin,
I think it was purely grammatical... they didn't want a period after each output.
I also checked if 'start' was greater than 0, which outputs 5 instead of 4, so maybe this helped.
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):
some_num = random.randint(1, 99)
even_odd(some_num)
if even_odd(some_num):
print("{} is even".format(some_num))
else:
print("{} is odd".format(some_num))
start -= 1
Damien Watson
27,419 PointsAs Jennifer and Steven said. :)
Steven Parker
231,248 PointsJustin directly tests the "truthiness" of start, which is what the challenge suggested.
Comparing it to 0 does the same job, but with extra verbage you don't need.
Damien Watson
27,419 PointsYeh, cool. Though it passed as well... picky on the period, not so much on how many times the loop runs. Thats humorous.
Justin Noor
3,692 PointsYou guys are frickin awesome!!! I passed the quiz.
I would have never thought of those periods...crazy hah. I also removed the unnecessary function callout.
Thanks guys!!!
Jennifer Nordell
Treehouse TeacherJustin Noor You're quite welcome!