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 trialUnsubscribed User
1,539 Pointseven.py - Make a while loop that runs until start is falsey.
I have been having issues trying to submit this challenge. The task asks:
Make a while loop that runs until start is falsey. Inside the loop, use random.randint(1, 99) to get a random number between 1 and 99. If that random number is even (use even_odd to find out), print "{} is even", putting the random number in the hole. Otherwise, print "{} is odd", again using the random number. Finally, decrement start by 1.
Issue: Every time I have tried to submit this I keep getting a "There was a communication problem". I have already tried contacting the help@teamtreehouse and they tried my exact code on their end and they don't get the communication problem message but they do get a "your task 1 is no longer passing" message.
Could there be something wrong with my code that is causing this?
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) == False:
print("{} is odd".format(num))
else:
print("{} is even".format(num))
start -=1
2 Answers
Bruno Correia
3,114 PointsHi Manuela,
You're almost there but remember indentation is important! If you want each iteration of the while loop to decrease -1 to start you need to put start inside the loop. This small change (note the indentation at the last line) should do the trick:
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) == False:
print("{} is odd".format(num))
else:
print("{} is even".format(num))
start -=1
Have fun learning!
P.S.: While testing your code, I did get the same communication error message and a suggestion to contact Treehouse supports. The code is indeed not correct but it looks like there is an issue with the error message, which can definitely be confusing!
Jennifer Nordell
Treehouse TeacherHi there! You're doing terrific! Your logic is spot on and you've hit all the challenge requirements ... so kudos! However, the "communication problem" is not a problem with Treehouse but rather with your code. Would it be reassuring to know that your code is off by one tab?
The problem here is indentation. Because you decrement start
. outside of the while loop it means that the while loop never finishes running. You have inadvertently created an infinite loop. This causes the "communication problem" error that you're receiving.
If I copy/paste your code into the challenge and indent the start -= 1
, your code passes with flying colors!
Good luck and keep coding!