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

Task 1 is no longer passing?

So, I understand that someone had moved the position of their code in order to get the challenge to acknowledge Task 1 as passing.

However I've tried every combination of moving my code about that comes to mind and I'm still just as stumped when trying to understand the logic of why Task 1 would no longer be passing.

Should the even_odd function not come first, as that is where I setup a function used later on in my while loop? While I would appreciate an answer, an explanation would be even more helpful.

Thank you so much.

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:
    x = random.randint(1, 99)
    if even_odd(x) != 0:
        print("{} is even".format(x))
    elif:
        print("{} is odd".format(x))
start -= 1

At this point I completed the challenge, although I'm still unaware as to how. I completed it by starting from scratch but unfortunately I didn't get a chance to copy down what correction made it correct. If anyone has any insight on which areas would have been my downfall, I would appreciate it!

1 Answer

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

I attempted the challenge and this is the code I passed with.

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:
    x = random.randint(1, 99)
    if even_odd(x):
        print("{} is even".format(x))
    else:
        print("{} is odd".format(x))
    start -= 1

I believe the problem you were having was that the challenge interpreter was likely confused by the if statement condition checking. since the even_odd(x) returns True or False as you know, depending on the value of x, it is enough to write

if even_odd(x):
    # block code

what you code would do is evaluate True != 0, or False != 0, depending on what even_odd(x) returns. In both cases you would get an evaluation down to True, or False, so it should in theory work, perhaps you could verify yourself by running it in the repl. I guess the syntactic checker in the challenge was just to ridged?????

That's my nest guess anyway, good to hear you got through the challenge.