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

I have functioning code, but am failing Task 1. Why is that?

I have tested this in workspaces and It works. I'm not sure why my import is holding me up.

even.py
import random
start = 5
def even_odd(num):
    return not num % 2
while True:
    num = random.randint(1,99)
    result = even_odd(num)
    if result == 0:
        print("{} is odd").format(num)
    else:
        print("{} is even").format(num)
    start -= 1
    if start == 0:
        break

1 Answer

Hi there,

Your code is mostly correct - the issue is where the parentheses are on the string formatting lines - right here:

if result == 0:
    print("{} is odd").format(num)
else:
    print("{} is even").format(num)

The closing parenthesis after the string is what's throwing it off - that parenthesis closes the print(), so it isn't correctly attaching .format to the string. If you move those parentheses to the end of the line, it should work. It'll look like this:

if result == 0:
        print("{} is odd".format(num))
    else:
        print("{} is even".format(num))

When it says a previous task is failing, it's usually a syntax error somewhere. One thing you can do to get a more specific error when that happens is to copy your code, then go back to the first task and put that code in - a syntax error will throw an error on that task as well, and it should give you something more specific.

Hope this helps!