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

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: