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

Ragy Abuamra
Ragy Abuamra
4,044 Points

After adding my code, it says that Task#1 is no longer passing!!

Need some thoughts here please.. Didn't change Task-1, just added the code below. And now it says that Task-1 is no longer passing! What am I doing wrong here? :(

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 start:
        if num % 2 == 0:
        print('{} is even'.format(num))
        else:
            print('{} is odd'.formate(num))
start -= 1

2 Answers

Ragy, in your While loop the variable num is not defined since it's only used in the even_odd function. You actually want to use the even_odd function in your if statement instead of if num % 2 == 0. You need to get a random number at the beginning of your whileloop for the even_odd function to test.

start = 5
while start:
    random_num = random.randint(1, 99)
    if even_odd(random_num):
       print('{} is even'.format(random_num))
    else:
        print('{} is odd'.format(random_num))
    start -= 1

Aaron you're not formatting the strings on your while loop returns

print("{} is even", some_amount) vs print("{} is even".format(some_amount))

Aaron Loften
Aaron Loften
12,864 Points

Thanks. I updated it. Same result though. It still says task 1 is no longer passing. Did you run your code through the challenge and get it to pass?

Oops there's actually one last thing I was gonna mention the start-- you have that's decrementing the variable doesn't work in python, you have to do start -= 1. Because the way python treats integers you have to reassign the variable. Just tried the code challenge and it's working after that last change. :)

https://stackoverflow.com/questions/1485841/behaviour-of-increment-and-decrement-operators-in-python

I've noticed that when you sometimes get errors in the code challenges it'll tell you that task 1 is no longer working, because of the general error. I usually don't pay mind to those and just run the code myself in a file on my local machine to find the real problem

Aaron Loften
Aaron Loften
12,864 Points

Wow. Youre right. Thanks. :D I appreciate the help. Bummer though on the increment/decrement thing. I love the simple life...haha. Thanks again.

Np!

Ragy Abuamra
Ragy Abuamra
4,044 Points

Thank you Bryan.. wasn't sure about it. Now it passed :)

Aaron Loften
Aaron Loften
12,864 Points

I just did this code challenge too and got the same error. I am not sure why.

It might be a bug. Maybe your friendly neighborhood moderator will come by and flag this for a fix. :D

--edit - My code works now because of Bryan's response -- heres my code

import random

start = 5

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

while start:
    some_amount = random.randint(1, 99)

    if even_odd(some_amount):
        print("{} is even".format(some_amount))
    else:
        print("{} is odd".format(some_amount))

    start-=1