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

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

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!

HI Alexey,

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

Alexander Antoshin , I updated my answer with solution, also look through the comments.

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

  1. Yes

  2. return not num %2 returns a boolean. Since even % 2 == zero or False and odd % 2 == not_zero or True. not just inverts it.

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