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 trialdrstrangequark
8,273 PointsNot sure what's wrong with my code
The challenge is:
Alright, last step but it's a big one. 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. I know it's a lot, but I know you can do it!
When I try the attached code, I get an error that says Task 1 is no longer passing (Task 1 was to import random). I'm pretty sure my code is correct. Any idea whats going on here?
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:
num = random.randint(1,99)
if even_odd(num):
print('{} is even'.format(num))
else
print('{} is odd'.format(num))
start -= 1
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! You've encountered the most common reason for receiving a "Task 1 is no longer passing" message. You've introduced a syntax error into your code which means that it can no longer be compiled/interpreted.
In short, you're off by one whole character. You simply forgot a colon on your else
statement.
You wrote :
else
print('{} is odd'.format(num))
But you meant to write:
else: #note the colon here
print('{} is odd'.format(num))
Hope this helps!
Niko Klanecek
3,152 PointsYour else
is missing a colon.
drstrangequark
8,273 Pointsdrstrangequark
8,273 PointsWow, I feel dumb. Thank you so much! I need to drill into my head to remember the colons!