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 trial

Python Python Basics (2015) Letter Game App Even or Odd Loop

Anders Axelsen
Anders Axelsen
3,471 Points

Task 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

even.py
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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles:

Anders Axelsen
Anders Axelsen
3,471 Points

Hi 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.

Why can't it be

while start True:
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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 as True.

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! :sparkles: