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 trialLaknath Gunathilake
1,860 Pointsunable to pass the challenge
I don't understand the part "Finally, decrement start by 1"
Also, I'm not sure, if the first part of the code I've written is correct
import random
start=5
def even_odd(num):
while start=True:
num=random.randint(1,99)
if num %2==0:
("{} is even".format(number))
else:
("{} is odd".format(number))
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
return not num % 2
2 Answers
hebendjd
7,626 PointsYou are off to a good start.
You have random imported
You gave the start variable a value of 5
Now:
-
You have the pre-made function even_odd that takes an argument num and returns: not num % 2
You don't need to edit this function. It is important to look at what this function is doing. As it is, even_odd will take num and return a value based on the rules that were given "# If % 2 is 0, the number is even. # Since 0 is falsey, we have to invert it with not." Take a moment to ponder exactly what value will be returned if num were equal to 2 or if num were equal to 3. This function becomes useful later.
"Make a while loop that runs until start is falsey." You have already given start a value of 5, making start inherently True. This loop is based on 2 ideas. 1: The loop having the condition of while. 2: The truthiness of start. You have the while but your check of start against the value of True is excessive because start is inherently True. You might think of it as asking: While start is equal to True, (while start==True:) but in logic, this is redundant because: start is True. All you really need are while and start.
You use random.randint(1, 99) inside your while loop. to get your random number.
Use the even_odd function to check if your random number is even or odd. When you pass your random number to even_odd, imagine that function call replaced with the value it will return. Just like step 4, avoid redundancy in your if statement.
Both your print statements are missing something very important and both have something undefined. I bet you can figure out what they are with a little review.
After all of these things in your while loop, the value of start should be decremented (minus equal) by 1.
So... start is 5 (making it True). while start:. You get a random number and check if it is even using your pre-made even_odd function. You print the first statement if the random number is even, else, you print the second statement. You reduce the value of start, making it 4. The loop repeats. And again. And again. And again. Now, you will have printed 5 statements, start will be decremented, recursively, to 0 and the while loop will end.
See how far this gets you and check back in with me if you are still stuck.
Allison Hanna
36,222 PointsSo that the loop will eventually reach a falsey value (in this case, 0) and stop running, decrement the value of the start variable by 1 each time. So inside your loop, after your if/else blocks,
start -= 1
Otherwise, start will always returns true and your'e stuck in an infinite loop.
Laknath Gunathilake
1,860 PointsLaknath Gunathilake
1,860 Pointshebendjd
7,626 Pointshebendjd
7,626 PointsYou're closer! Try leaving the even_odd function and your while loop independent of each other. There is no need to nest one in the other when you can just call even_odd using your random number from your while loop.
Laknath Gunathilake
1,860 PointsLaknath Gunathilake
1,860 Pointshebendjd
7,626 Pointshebendjd
7,626 PointsWithout giving you the answer, let me see if I can get you pointed in the right direction. The even_odd() function is fine the way that it comes.
Laknath Gunathilake
1,860 PointsLaknath Gunathilake
1,860 Pointsso its
if num%2=='0':
I was able to pass the challenge. Thank you
hebendjd
7,626 Pointshebendjd
7,626 Pointsif num % 2 == 0: doesn't use your even_odd() function, which is a specification of the exercise.