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

Wrong number of prints...?

why am i getting this eror message

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

1 Answer

pooya tolideh
pooya tolideh
12,184 Points

I think you misunderstood the falseness of number zero.

Zero is considered false in a conditional statement, but that doesn't mean it is the same data type as boolean.

Also, any integer except zero is considered truthy by python. So you don't need to invert zero.

not operator must be only used inside a conditional statement or with a variable that stores booleans -- False and True

So this is how your code needs to be re-written:

start = 5
def even_odd(num):
    # Any number but 0, is considered truthy and therefore, odd. No need to invert it
    return num % 2

while start :
    # as long as 'start' is not zero, the loop runs

    X=random.randint(1,99)
    Z = even_odd(X)
    if Z:
        print( "{} is even".format(X))
    else:
        print("{} is odd".format(X))
    start -= 1