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

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
231,275 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
231,275 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