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

Didier Borel
Didier Borel
2,837 Points

even odd

every time i get to the 3 rd step , I get the error message, task 1 no longer passing. can someone help me pls. what is wrong with my code

even.py
import random
start=5
def even_odd(num):
if num % 2==0:
    return True
    else:
    return False
    while randon.randint(1,99)==True:
        print ("{} is even").format.random.randint(1,99)
    else:
        print ("{} is odd").format.random.randint(1,99)
    start=start-1

2 Answers

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)
    if even_odd(num):
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))

    start -= 1
Didier Borel
Didier Borel
2,837 Points

Alex, thanks, I stiill have 2 questions where do python get the even_odd ? we never defined it. how did it know how to run the even_odd function? and how can we first define start=5, and then say while start:..... I don't understand the logic of saying a variable is equal to something, and then saying, that as lond as a variable is something else, do this, and then do that.

thxs for your help

For your first question, the code challenge defined the even_odd function already. That stuff between the import random and start = 5 is the function definition. For your second question, 0 is "Falsey" and all other numbers is "True".

Take a look at this:

# This is the same...
number = 10
while number != 0:
    print("Hello!")
    counter -= 1

# ...as this
number = 10
while number:
    print("Hello!")
    counter -= 1

Hope that helps! :dizzy: