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

even_odd function

am getting a 'Wrong number of prints ' message after writing the attached code and am stuck

even.py
import random
start = 5
while start < 5:
        secret_num = random.randint(1, 99)

        if even_odd(secret_num):
            print("{} is even.").format(secret_num)
        else:
            print("{} is odd.").format(secret_num)
        start -= 1


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

2 Answers

Your print statement has an error

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

The format method is called on the String as show below:

print("{} is even.".format(secret_num))
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Clainos,

Your problem is in the loop definition.

Your code says:

start = 5
while start < 5:

Since you made start 5 and your program only enters the loop if start is less than 5, your loop will never execute.

Note that the task tells you to "Make a while loop that runs until start is falsey." This is telling you that your loop should run until start has a value that Python considers False. We know that start is an integer, so what types of integer are False? You can check this yourself in the Python interpreter by typing something like:

5 == True

The interpreter will come back and tell you that is True. Similarly:

1 == True

Interpreter will again tell you that is True. What about:

0 == True

This time, Python will tell you that is False. So we know that 0 is a falsey value.

The body of your while loop decrements start at the end of every iteration, so your value of start will reach 0, at which time it will be False.

Therefore, you can set your while loop to run until start is False. This is as simple as writing:

while start:

Hope that clears everything up.

Cheers

Alex

changed that and now getting the message 'Task one is no longer passing'