Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Even or Odd Loop

what is wrong number of prints

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

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

3 Answers

Steven Parker
Steven Parker
216,136 Points

If you look at these two lines:

start = 5
while start == 0:

You can see that "start" will be 5 when the loop tests to see if it is equal to 0, so the loop will never run.

You could change the comparison, but even easier than that is to take advantage of the fact that a number that is not zero is considered "truthy" and just test it by naming it:

while start:

Similarly, you never have to compare anything to "True" — you can just name it.

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

This too gives an incorrect answer Some or all of your prints are wrong

Steven Parker
Steven Parker
216,136 Points

You have changed the way you call and test the "even_odd" function. Since it always returns a boolean response (True or False), it will never equal 0.

The original code only needed the "while" fixed to pass. The other comment I made about never needing to compare anything with "True" could be implemented like this:

    if even_odd(num):

Thanks! that helped