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 trialJordan Bailey
720 PointsGetting "Wrong number of prints," Don't understand what is wrong
The code looks right compared to the example given, don't know if it's a syntax issue I'm missing or if I'm just not seeing what is wrong with my conditions. Please help, and thank you!
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 is True:
num = random.randint(1,99)
if even_odd(num) is True:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
3 Answers
Charlie Gallentine
12,092 PointsI am not sure that I found the problem, however, by removing the "is True" parts from the while loop and if statement I got the code to pass. In my text editor with the code as given, nothing is printing out.
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(1,99)
if even_odd(num):
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
EDIT: I checked a bit more, the "is True" in the while loop keeps anything from printing, I'm not sure why though
Jennifer Nordell
Treehouse TeacherHi there! Charlie Gallentine is correct. It is the is True
part that is keeping anything from printing. The variable start
is never equal to True. Here we get into the difference of "truthy" versus True
. If you are checking for True
then the value must explicitly be set to True
.
When we say while start:
we say "do this while the value is truthy". A number that is not 0 will be considered "truthy",. When we decrement the value of start so that it eventually reaches 0 then that will cause while start:
to evaluate as false and the loop will end.
When you start the while loop with while start is True:
, it will evaluate to false and never run the loop as start
was never set to True
.
Hope this explanation helps!
Jordan Bailey
720 PointsThank you both for your help!! It works now, and I appreciate the explanations as well.