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 trialAlice Ng
1,560 PointsCan't pass this challenge. PLSSS Help.
gave me an error of "Wrong number of prints". Sorry i have only just started programming, it has taken me hours to figure out what is wrong with my code. Pls give some advice. Thanks
import random
start = 5
while start == True:
rand1 = random.randint(1,99)
if even_odd(rand1)==True:
print("{} is odd".format(rand1))
else:
print("{} is even".format(rand1))
start = start - 1
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
1 Answer
Stuart Wright
41,120 PointsThere are two issues to fix here:
while start == True:
This is a bit tricky to get your head around, but although positive integers such as 5 are considered 'truthy', they are not considered equal to True. 5 == True evaluates to False in Python. You can simply use:
while start:
The only other issue is that your even and odd print statements are the wrong way around.
Ryan S
27,276 PointsRyan S
27,276 PointsHi Weihao, just one more thing to add to Stuart's answer: the code is read from the top to the bottom. In your while loop, you are trying to call the "even_odd" function, but it is not defined until later in the code. You will need to make sure the function definition comes before the while loop.
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsHere's one more thing to add:
If you see somewhere in your code that you're doing
== True
, it's better to just leave that off entirely, whether it be in a conditional statement or a loop. The same goes forFalse
. If you see yourself doingsomething == False
, it's better to writenot something
. For example:Alice Ng
1,560 PointsAlice Ng
1,560 PointsThanks guys