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 trialGerald Wells
12,763 Pointsissue on 3rd part of challenge, not sure how to decrement start, also not 100 if my code is correct
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!
import random
start = 5
def even_odd(n_num):
while start == True:
n_num = random.randint(1,99)
new_num = n_num % 2
if new_num != 0:
print("{} is odd".format(n_num))
else:
print("{} is even".format(n_num))
#return not % 2 ----- not sure what to do with this pre-inserted code
else:
start -1
#while loop >>>
#make a random int call 1-99 >>>
#print if number is even/even_odd >>>
#decrement start by 1
even_odd(n_num)
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThe intent of the challenge is to used the function even_odd
in a while
loop:
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
# Make a while loop that runs until start is falsey.
while start:
# Inside the loop, use random.randint(1, 99) to get a random number between 1 and 99.
n_num = random.randint(1,99)
if even_odd(n_num):
# If that random number is even (use even_odd to find out)...
# print "{} is even", putting the random number in the hole.
print("{} is even".format(n_num))
else:
# Otherwise, print "{} is odd", again using the random number.
print("{} is odd".format(n_num))
# Finally, decrement start by 1.
start -= 1
Gerald Wells
12,763 PointsI figured that part out actually, my issue was something a bit more obvious than that... apparently I indented the while block, I am an idiot. Thank you both Chris and Evan for your help!
Gerald Wells
12,763 PointsGerald Wells
12,763 PointsI understand now what it was asking and I thank you for your help, however when I run this code in the challenge it fails, when I run the same thing in my terminal it is perfect, there are no variations in either script. Did you run this in the challenge?
Evan Demaris
64,262 PointsEvan Demaris
64,262 PointsHi Gerald,
Chris' code is just missing the amount to decrement
start
by at the very end. If you enter 1, then his code will pass, like so;Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsSorry. Cut-and-paste error missed the final 1. It was run in the challenge and passed. Thanks Evan for catching this.