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 trialAnders Axelsen
3,471 PointsTask 3: why do I get this unusual kind of error message?
This is a common error message I get: http://imgur.com/a/9OALU
I tried making the (num) correspond to even_odd(num). Is it because I am logged in in two different browsers?
- Can you specify a problem in my code?
I have had this problem for two days now. :-) Any help would be greatly appreciated.
Kindly, Anders
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 > 0:
num = random.randint(1, 99)
if even_odd(num):
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! The reason there is a "communication problem" is because you've created an infinite loop. You state that it should run while start is greater than 0. To be honest, this could be abbreviated to while(start):
as start will become "falsey" when it hits a value of 0. But the problem here is the decrement. The decrement doesn't happen until after the loop completes which means that start will always be 5 and the loop never ceases execution. To fix this, indent your decrement of start inside your while loop.
Hope this helps!
Anders Axelsen
3,471 PointsAnders Axelsen
3,471 PointsHi Jennifer
First off. I actually learned a lot, being stuck on this one. And then; your explanation made perfect sense. I think, I am getting a hang of this loopy side of business.
Hopefully, I won't be initiating any black holes in the near future!
Thanks for the explanation.
ryan smith
687 Pointsryan smith
687 PointsWhy can't it be
while start True:
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi Ryan! It's because that's not valid syntax. It needs an evaluation here. Now, you could do something like:
while start == True:
But that won't quite work with the given code either. It's comparing against an explicit value of
True
instead of a "truthy" value. The number 5 is "truthy", but it's not exactly the same asTrue
.However, if you wanted to make the condition really short, you could do what I did.
while start:
This says continue doing this loop until start is no longer "truthy". Given that 0 is a "falsey" value, the loop will cease execution when
start
runs down to 0.Hope this helps!