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 trialTomasz Necek
Courses Plus Student 7,028 PointsMake a while loop that runs until start is falsey.
Can anyone explain me what is wrong ? :) Thank you
import random
start = 5
while True:
num = random.randint(1, 99)
def even_odd(num):
return not num % 2
print("{} is even".format(num))
else:
print("{} is odd")
start -= 1
5 Answers
Josh Keenan
20,315 PointsChange the while condition, to while start does not equal 0!
Josh Keenan
20,315 PointsHere is Chris Freeman's solution, this should explain a lot better than I can
Python Python Basics Letter Game App Even or Odd Loop
about 1 month ago by Gerald Wells 4,316
issue 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)
Add Comment
2 Answers
2
MOD
Chris Freeman 26,776 about 1 month ago
The 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
Tomasz Necek
Courses Plus Student 7,028 PointsStill not working. Oops! It looks like Task 1 is no longer passing.
Josh Keenan
20,315 PointsOh yeah the loop should be in the function not the other way around,
Emmanuel Koranteng
21,488 PointsChange while condition to while start != False:
Josh Keenan
20,315 PointsIn this case this is wrong as the loop won't run still, we do until start is equal to 0 as 0 can also mean false.