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 trialJordan Bester
4,752 Pointsoutside of the even_odd function, named start that's assigned a number
little confused on this one? help
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
5 = 'start
2 Answers
Nathan Tallack
22,160 PointsOk. This one was a tough one. You did right with the first task of importing random. For the second task you want to declare the value for start before the even_odd function is declared, so you put that above and you write it like start = 5 so that you are saying the value label start is equal to the integer 5. Take care not to use quotes around the number or it becomes a string rather than an integer.
The last task is a tough one. You end up with code that looks like this if you follow the instructions carefully. I've commented the code so you can understand how it relates to the task instructions.
import random # Task 1
start = 5 # Task 2
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: # Will loop until start == 0
number = random.randint(1, 99) # Get a random number
if even_odd(number): # Use the above function to check for even
print("{} is even".format(number)) # This prints out even numbers.
else:
print("{} is odd".format(number)) # This prints out odd numbers.
start -= 1 # Decrement loop counter by 1
So this will run until the start variable finally reaches 0 and then the while loop will end and your code is done. :)
Steven Parker
231,269 PointsHere's a few hints for task 2:
- "outside of the even_odd function" means your statement will not be indented
- the variable name must occur before the =
- the value being given to the variable will occur after the =