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

Alright, last step but it's a big one.

Hello lads,can anybody help me? It seems that my code is right, but the challenge didn't accept that

even.py
import random
start = 5

while start > 0:
    rand_number = random.randint(1,99)
    start -= 1
    if even_odd(rand_number) == True:
        print("{} is even".format(rand_number))
    else:
        print("{} is odd".format(rand_number))


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

3 Answers

Reyam Marcos
PLUS
Reyam Marcos
Courses Plus Student 10,367 Points

try this one,

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

hope it helps

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Jirka,

I used the following code to pass this challenge:

while start:
    randomNumber = random.randint(1, 99)
    if randomNumber % 2 == 0:
        print("{} is even".format(randomNumber))
    else:
        print("{} is odd".format(randomNumber))
    start -= 1

The start variable needs to decrement by 1 after the if statement and the if statement needs to check if the number is even by using modular (%). Hope this helps! Happy Coding!!!

Okay :) thank you guys