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

Why is Task 1 no longer passing

This works in Repl, so why is it not working here?

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

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

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I can't say why it works in your REPL because it gives me a syntax error. Your parentheses on the print statements are a little off. The format method should be used immediately at the end of the string to be formatted, but you have a closed parenthesis there.

For example, you wrote:

print("{} is even").format(rand_num)

But this should be:

print("{} is even".format(rand_num))
# note the movement of the closed parenthesis after the string

When I fix the placement of the parentheses in both of your print statements, the code passes the challenge! :sparkles:

Stuart Wright
Stuart Wright
41,118 Points

Usually when it says task 1 is no longer passing and you haven't changed anything relating to task 1, it's because there's a syntax error somewhere that stops the code from executing, and that's the case here:

    print("{} is even").format(rand_num)
    print("{} is odd").format(rand_num)

You need to call the .format() method on the string itself, rather than on the result of the print function (which returns None). You can achieve this by moving the ) from immediately after your closing " to the end of the line.

Thanks! That is strange that Repl didn't throw a syntax error.