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 trialJonatan Spahn
6,362 PointsCan some one explain this for me, thank you!
Hello can someone explain the code for me I get most of it, but I'm not making the complete connection. I'll go line by line, and yeah this might be silly but I don't want to proceed with out fully understanding it.
import random # so here we are importing from a library
def even_odd(num): # I get that you always get a 0 if its an even number, and that since 0 is traditional = to a false value we bring in the not. So if the num is 10 we would get 0 so we would return True? if it was if their was a remainder of 1 we would return False?
# 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 # setting up the counter for the while loop so it can eventually turn false
while start != 0: num = random.randint(1, 99) if even_odd(num):
at this point num will be a random number that goes into the even_odd function. If its even it will return a True value, but how does it know that an even correlates to the first print? Does the script assume true until it is false? and so once it gets a false value it goes to the else section of the statement?
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
Hopefully my rambles are a little coherent and again, thank you for the 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
start = 5
while start != 0:
num = random.randint(1, 99)
if even_odd(num):
print("{} is even".format(num)) # <-- sentence period removed
else:
print("{} is odd".format(num)) # <-- sentence period removed
start -= 1
Chris Freeman
Treehouse Moderator 68,441 PointsPromoting Zachary's comment to Answer.
1 Answer
Zachary Williamson
Python Web Development Techdegree Student 3,347 PointsI'll do my best, I think I know where your confusion lies:
def even_odd(num): # I get that you always get a 0 if its an even number, and that since 0 is traditional = to a false value we bring in the not. So if the num is 10 we would get 0 so we would return True? if it was if their was a remainder of 1 we would return False?
You've pretty much got it spot on here. As the challenge comments say, since 0 by default is False, you have to make it True.
at this point num will be a random number that goes into the even_odd function. If its even it will return a True value, but how does it know that an even correlates to the first print? Does the script assume true until it is false? and so once it gets a false value it goes to the else section of the statement?
The way loops operate, is you're basically going through each condition in order. So if your program generates a number that you know is odd (and will return false), the way the code is structured it will still check if it's even first:
if even_odd(num): #this is the first condition, so it will test if the number is even first
print("{} is even".format(num)
else: #if it's not even, it moves to the next condition
print("{} is odd".format(num))
I hope that makes sense. For each condition you have in an if
loop (if, elif, elif, else)
, it goes through each condition in order to check if that condition is true.
gyorgyandorka
13,811 Pointsgyorgyandorka
13,811 PointsZachary Williamson Just a technical sidenote about using the forums: instead of adding comments, I suggest posting regular answers, because they can be upvoted and marked as "answered" and others can see them when browsing through the questions (like "OK, no further help needed here").