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

Paulhan Carre
Paulhan Carre
1,368 Points

when i get to the 3rd task i get a message saying there's a communication error and i don't know what to do

someone help

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 True:
    thing = random.randint(1, 99)
    if even_odd(thing):
        print('{} is even'.format(thing))
    else:
        print('{} is odd'.format(thing))
start -=1

1 Answer

Jimmy Smutek
seal-mask
.a{fill-rule:evenodd;}techdegree
Jimmy Smutek
Python Web Development Techdegree Student 6,629 Points

Hi Paulhann,

You're close but there are a few issues with your code.

First is line 7, while True:

The problem here is that there is not a condition for Python to evaluate, the condition does not break or return false, and the compiler goes into an endless loop. Unfortunately the error messages from the Treehouse editor are rarely helpful, but more than likely this is what is causing the communication error.

Consider what we are asked to evaluate - while what is true?

The instructions say "Make a while loop that runs until start is falsey." So we need the loop to decrement by 1 each time it runs, until start is 0 (ie. false).

So we need to evaluate the start variable, whether or not it is false. As long as start does not equal 0 it will return true. Right? We might say:

while start != 0:

...or even

while start:

The second problem is the indentation on line 13. Even after you've fixed the issue on line 7 the compiler is still going to go into an endless loop - now because the command to decrement start is outside of your while loop. Ie:

while something:
    ## I'm inside the loop
## I'm outside the loop, and unreachable

If you fix these two issues your code will execute correctly and should pass.

The other thing I wanted to point out is your formatting. There's a number of issues in your formatting and, mundane though it may sound, I've found that paying attention to formatting has helped me to catch errors like the indentation issue on line 13.

I'd suggest doing the work on your own computer with a good text editor or IDE. It'll save you a lot of frustration. I use Pycharm, it's fantastic. There's a free version called PyCharm Community, but if you want something liter, or don't want to spend any money onPyCharm Professional, I'd suggest Atom with the Beautify and python-autopep8 packages installed.

Anyway, hope the above helps. Please post back if you are still having issues.

Paulhan Carre
Paulhan Carre
1,368 Points

Thank you was stuck for days, that was a good explanation thank you