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 trialKaren Hensley
Courses Plus Student 577 PointsNot checking and do not know why. Checker message is bogus.
I am getting progressively frustrated with the code checker.
It points to "Task 1 is no longer working" but nothing has changed with that portion of the code (i.e, import random).
Also - the next task - "start = 5" is fine as well.
It is my while loop it does not like, but I have no clue why. I feel like I am reading what is asked, coding that way - and the checker gives me no clue as to why it fails.
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 True:
num = random.randint(1, 99)
if even_odd(num) = True:
print("{} is even").format(num)
else:
print("{} is odd").format(num)
start = start -1
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHi Karen,
The "Task x is no longer passing" is a very misleading error. 99.9% of the time, it has nothing to do with the task it states, but rather, that you have introduced a syntax error in the current task you are on. So, ignore what is says and look at the code you have most recently entered.
Here, it is your while loop
. You are on the right track, but there are a few things going wrong here:
- You have a loop that will never end (an infinite loop). The code
while True
will always be True because there is no code to change it to False. Instead, you need to be checking theTruthy
value of thestart
variable, not a hard coded Boolean. - In the
if statement
, the code is Trying to assign a boolean to a boolean, which will throw a syntax error. The first partif even_odd(num)
is already checking to see if the function call returns a True or False and will use this value, but when you add= True
you are trying to assign aBoolean
to the statement (remember = is an assignment symbol) - The
print
methods have misplaced parentheses. The.format()
need to be inside of the method, but you closed the method before the.format()
. Theprint
method's closing parenthesis need to be after the.format()
. - The decrementation of the variable needs to happen outside of the
if/else
clause. Right now, the variable will only be decremented if theelse
part runs but not theif
.
I have provided the corrected code for you to review. Have a look as you go through the errors. I hope this all makes sense.
while start:
num = random.randint(1, 99)
if even_odd:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start = start -1
Keep Coding! :)
Karen Hensley
Courses Plus Student 577 PointsKaren Hensley
Courses Plus Student 577 PointsThank you for your help. Two concepts I am trying to understand.
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsHey Karen,
Hopefully, I can help clear those up a bit for you.
start
equals 0. The reason is thewhile start:
is checking the value of the variablestart
. Any positive number will return a 'truthy' value, and keep the loop running. Once it hits zero (a falsey) value, the loop stops. I'm not sure exactly where or when this was covered, sorry. So, if you just putwhile 0:
it would be the same as puttingwhile False:
.So, the number_generated is storing the random number, which then gets passed into the function, checked and returned and used in the if/else.
I hope this helps. :)
Again, sorry for the confusion on my error.