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

Alexander Antoshin
Alexander Antoshin
1,528 Points

Need help with the quiz "even_odd"

Hey guys,

Can someone check my code and tell what I'm doing wrong?

even.py
import random
start = 5

def even_odd(num):
    while True:
        if radnom.randint(1, 99) % 2 == 0:
            print("{} is even".format(random.randint(1, 99))
        elif:
            print("{} is odd".format(random.randint(1, 99))
        else:
            return not num % 2
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    start -= 1

1 Answer

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Hi, Alexander Antoshin !

Well here is the solution.

import random  # Import random library
start = 5  # Create start variable

def even_odd(num): 
    return not num % 2  # This returns True for even and False for odd

while start:
    num = random.randint(1, 99)  # You must only run random.randint(1, 99) ONCE. Because each time it woulod return a new random.
    if even_odd(num):  # You check num.
        print("{} is even".format(num)) # Print this if function returns True
    else:
        print("{} is odd".format(num))  # Print this if function returns False
    start -= 1

Does it make any sense? If you need more help - just ask!

Alexander Antoshin
Alexander Antoshin
1,528 Points

HI Alexey,

Fixed the issues but still doens't work. Can you please check the code one more time?

Alexander Antoshin
Alexander Antoshin
1,528 Points

In general I got it but still have some questions:

  1. Am I right that while loop and even_odd function are in separate blocks?
  2. Can't understand why "return not % 2" returns True if even and False if odd.
Alexander Antoshin
Alexander Antoshin
1,528 Points

Разобрался, спасибо!