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 trialNikhil Alexander
1,444 Pointsim cannot seem to correct this error...its saying the code took to long to run
i do not know whether to keep the while True as it or change it according to something in the code challenge
import random
start = 5
while True:
def even_odd(num):
num = random.randint(0, 99)
if num % 2 == 0:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
return not num % 2
start -= 1
even_odd()
Nikhil Alexander
1,444 Pointsthe while condition would not be true when the number 5 becomes 0
3 Answers
Ken Alger
Treehouse TeacherNikhil;
Okay, let's back up a minute as it seems you're overall off track a bit, which is fine, this stuff isn't super intuitive.
At the end of Task 2 your code should, more or less, look like this, yes?
import random
start = 5
def even_odd(num):
return not num % 2
And here are the instructions for Task 3:
Alright, last step but it's a big one. Make a
while
loop that runs untilstart
is falsey. Inside the loop, userandom.randint(1, 99)
to get a random number between 1 and 99. If that random number is even (useeven_odd
to find out), print"{} is even"
, putting the random number in the hole. Otherwise, print"{} is odd"
, again using the random number. Finally, decrementstart
by 1. I know it's a lot, but I know you can do it!
Let's take those steps one at a time.
- Make a
while
loop that runs untilstart
is falsey. Recall that a zero value meets the condition so we can either dowhile (start > 0): ...
or we could simply dowhile start: ...
import random
start = 5
def even_odd(num):
return not num % 2
while start:
- Next we have the code for our number generator and we can assign that to a variable name.
num
seems as good as any.
import random
start = 5
def even_odd(num):
return not num % 2
while start:
num = random.randint(1, 99)
- If our random number,
num
is even we want to print that it is. Same for an odd number. We are instructed to use oureven_odd
method to do so. So we don't need to change that method at all.
import random
start = 5
def even_odd(num):
return not num % 2
while start:
num = random.randint(1, 99)
if even_odd(num):
// print our even output
else:
// print our odd output
- Finally, we decrement
start
by 1. That should be doable.
import random
start = 5
def even_odd(num):
return not num % 2
while start:
num = random.randint(1, 99)
if even_odd(num):
// print our even output
else:
// print our odd output
start -=1
That should do it from a logic standpoint. I'll leave it to you to complete the print statements. ;-)
Post back if you're still stuck, Nikhil. Great job sticking with this as well. You got this!
Ken
Nikhil Alexander
1,444 PointsThank you so much for sticking on this task for me and helping me... ive completed the pythons basic course.... now im movin forward... once again thank you Ken. ~NIKX222
Ken Alger
Treehouse TeacherNikhil;
Actually, the way the above code is written, the loop will never end. You would need to check the value of start
to achieve the results you want. Something along the lines of:
start = 5
while (start > 0):
...
start -= 1
Doing while True:
runs forever unless there is a break
statement.
Ken
Nikhil Alexander
1,444 Pointsthank you ken, i understand that part...... but when i write (start > 0)....it says task one is no longer passing... i know im making some mistake.. could you help me find and and correct that??
Ken Alger
Treehouse TeacherCan you please post the most current version of the code your using in this challenge?
Thanks, Ken
Nikhil Alexander
1,444 Pointsimport random
start = 5
while (start > 0):
def even_odd(num):
num = random.randint(1, 99)
if num % 2 == 0:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
return not num % 2
start -= 1
even_odd()
I even tried using
while (int(start) > 0):
Ken Alger
Treehouse TeacherDoh! I just noticed that you are defining your even_odd
function inside the while loop.
I would define even_odd()
separately:
def even_odd(num):
return not num %2
And then call that inside your while loop. Sorry for the confusion on that.
Ken
Nikhil Alexander
1,444 Pointsthank you for helping me... but im sorry to bother you...the code still doesnt work could you help me out??
import random
start = 5
def even_odd(num):
num = random.randint(1, 99)
if num % 2 == 0:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
return not num % 2
while (start > 0):
even_odd(num)
start -= 1
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherNikhil;
Based on the code you posted, can you give me an example of when the
while
condition would every not be True?Ken