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 trialJonathan Corona
716 PointsMy code is not passing, it says task 1 is no longer correct even though I did not change start = 5.
Not sure why task 1 is not passing when I am on task 3 of the challenge question.
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 == True:
randomnum = random.randint(1,99)
if randomnum is even_odd():
print("{} is even".format(randomnum)
else:
print("{} is odd".format(randomnum)
start - 1
2 Answers
Steven Parker
231,269 PointsIntroducing a syntax error invalidates the entire script, so when the challenge re-tests task 1 it seems to fail there. There's actually 3 syntax errors, the structure of the *if" statement, and both print statements are missing a final close parenthesis.
The "if" statement should pass the value as the argument to even_odd, and test the response directly:
if even_odd(randomnum):
Also, start will never be equal to "True" since it is a number value. But any number other than 0 will test as "truthy" just by naming it in the test (like "while start:
").
Finally, the subtraction operator ("-
") doesn't change the variable it is used on, but a subtraction assigment ("-=
") would.
Jonathan Corona
716 PointsI get it now, thank you. So if you have a function that has two arguments like:
def name(first,last):
Does this mean that this function must always be ran with two values?
Steven Parker
231,269 PointsYou got it!
Jonathan Corona
716 PointsJonathan Corona
716 PointsThank you I have corrected the script here: If it wasn't for you, I don't think i would understand how using a function with a randomnum would work. I still don't understand what adding an argument to the function does, for instance num, if we used randomnum in its place.
example:
def even_odd(num):
then later:
if even_odd(randomnum):
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThe first example (with the word "def") is the function definition, and the parameter name serves as a "placeholder" for the value that will be passed to the function when it is called.
The second (in the "if" statement) is where the function is called, and the value of "randomnum" is passed in as the argument that the function will actually operate on.