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 trial

Python Python Basics (2015) Letter Game App Even or Odd Loop

Karen Hensley
PLUS
Karen Hensley
Courses Plus Student 577 Points

Not 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.

even.py
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
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi 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:

  1. 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 the Truthy value of the start variable, not a hard coded Boolean.
  2. In the if statement, the code is Trying to assign a boolean to a boolean, which will throw a syntax error. The first part if 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 a Boolean to the statement (remember = is an assignment symbol)
  3. The print methods have misplaced parentheses. The .format() need to be inside of the method, but you closed the method before the .format(). The print method's closing parenthesis need to be after the .format().
  4. The decrementation of the variable needs to happen outside of the if/else clause. Right now, the variable will only be decremented if the else part runs but not the if.

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! :) :dizzy:

Karen Hensley
Karen Hensley
Courses Plus Student 577 Points

Thank you for your help. Two concepts I am trying to understand.

  1. Does the "while start:" loop automatically stop when start = 0? That was not clear from the training. So, you are saying that a "while 0:" forces a break?
  2. I guess I am confused about just calling the function even_odd and not doing some if "assessment" on what the function returns. The function returns the TRUE/FALSE, the if is just saying what to do when TRUE and the else is saying what to do if FALSE.
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hey Karen,

Hopefully, I can help clear those up a bit for you.

  1. Yes, the while loop will stop when start equals 0. The reason is the while start: is checking the value of the variable start. 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 put while 0: it would be the same as putting while False:.
  2. You are correct to be confused about this. My apologies. I didn't realized that I missed the parameter when I pasted the code block in. You're right, now it's not checking any numbers (which is confusing as to why that passes the challenge then??), but anyways, yes, you do need to pass in the random number being stored in the variable. Here it would also be a good idea to have a different name than that parameter of the function. See below for the corrected snippet.
while start:
    number_generated = random.randint(1, 99)
    if even_odd(number_generated):
        print("{} is even".format(number_generated))
    else:
        print("{} is odd".format(number_generated))
    start = start -1

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.