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

Alice Ng
Alice Ng
1,560 Points

Can't pass this challenge. PLSSS Help.

gave me an error of "Wrong number of prints". Sorry i have only just started programming, it has taken me hours to figure out what is wrong with my code. Pls give some advice. Thanks

even.py
import random
start = 5

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

1 Answer

Stuart Wright
Stuart Wright
41,120 Points

There are two issues to fix here:

while start == True:

This is a bit tricky to get your head around, but although positive integers such as 5 are considered 'truthy', they are not considered equal to True. 5 == True evaluates to False in Python. You can simply use:

while start:

The only other issue is that your even and odd print statements are the wrong way around.

Ryan S
Ryan S
27,276 Points

Hi Weihao, just one more thing to add to Stuart's answer: the code is read from the top to the bottom. In your while loop, you are trying to call the "even_odd" function, but it is not defined until later in the code. You will need to make sure the function definition comes before the while loop.

Michael Hulet
Michael Hulet
47,913 Points

Here's one more thing to add:

If you see somewhere in your code that you're doing == True, it's better to just leave that off entirely, whether it be in a conditional statement or a loop. The same goes for False. If you see yourself doing something == False, it's better to write not something. For example:

something = True

if something:
    pass #This code will run
else:
    pass #This code will not


other_thing = False

if not other_thing:
    pass