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 trialFerdinand Pretorius
18,705 PointsNeed help with python challenge (Treehouse is drunk would be more accurate)
Apparently I'm doing something wrong on task 3
When i run my code locally in gnome shell using python 2 and 3 it executes without any problems, gives 5 random numbers and then the while loop becomes falsey and it quits, as expected!
Still when i do the challenge i keep getting the error: Oops! It looks like Task 1 is no longer passing...
Here is the description!
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.
If that random number is even (use even_odd to findout), 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!
And here is my code!
import random
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
start = 5
while start:
rnum = random.randint(1, 99)
if even_odd(rnum):
print "{} is even".format(rnum)
else:
print "{} is odd".format(rnum)
start -= 1
2 Answers
tobiaskrause
9,160 PointsNot sure what is wrong in your code this worked for me
import random
def even_odd(num):
return not num % 2
start = 5
while start:
num = random.randint(1, 99)
if even_odd(num):
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
Python 3 has a new print function...but i am not sure if that was the problem
Hanifah W
395 PointsWhy the variable 'start'. Not sure what it does here? Wouldl appreciate any clarity someone can give,
John Judge
3,527 PointsHanifah W I believe start is acting as a countdown. It 'starts' at 5, and each time the while loop iterates decrements by 1. Once it hits 0 it becomes falsey and breaks the loop.
Sharon Walls
9,234 PointsYou need "start" to act as a countdown from 5.
Ferdinand Pretorius
18,705 PointsFerdinand Pretorius
18,705 PointsI didn't even think to use the print function, i thought PEP 3105 print statement would still be valid in the test, turns out i was wrong.
Cheers for info, this worked.