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 trialJeetender Batra
Courses Plus Student 3,028 PointsMake a while loop that runs until start is false. Need explaination while start>1: ?
Need explanation
import random
start=5
while start>0:
temp_num=random.randint(1,99)
if even_odd(temp_num):
print("{} is odd".format(temp_num))
else:
print("{} is even".format(temp_num))
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
4 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsAt the start of each while
iteration, the condition start > 0
is checked. if the value of the start
variable is greater than zero, another pass through the block of code is performed.
In the code posted, start
has an initial value of 5, so the while loop will execute. At the end of the code block, the statement start +=1
will increment start
to be 6. As you can see, the value of start
keeps increasing and will never drop below 0. Hence, the while
loop will never end.
The last while
block statement should decrement start
so that after 5 iterations, it will be 0. Try changing it to read start -= 1
Jeetender Batra
Courses Plus Student 3,028 PointsThank you Chris.
looks like. looks like a bug in treehouse.
Chris Freeman
Treehouse Moderator 68,441 PointsBugs are rare in Treehouse challenges. Post back if you're still having issues with it.
Jeetender Batra
Courses Plus Student 3,028 PointsYep, I have sent an email to support team for help with screenshot. Thank you Chris..!
Chris Freeman
Treehouse Moderator 68,441 PointsI don't see the error. Can you be more specific on what you think the bug is?
Jeetender Batra
Courses Plus Student 3,028 PointsHello Chris,
Actually the message shown on the challenge was in correct. problem was i have defined function below while loop. function definition should come first.
Chris Freeman
Treehouse Moderator 68,441 PointsYes. The error messages returned by the challenges can be vague or general. From what I understand, the issue is a reduced granularity in the information passed to the checker on some code failures within the challenges. The Challenge checker code then has to make the best of what is reported. This sometimes leads to the generic "Bummer" message when a more detailed message would have been more helpful.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsOne other error in the posted code is the function
even_odd
is referenced before being defined. Thewhile
loop code must come after the definition of theeven_odd
function.