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

Even_odd task

Hi guys,

I have been using this code below to print out whether a random number is odd or even. However, the task consistently says that I have entered a wrong number of prints. Can anyone help please?

import random

start = 5

def even_odd(num): while True:

    num = random.randint(1,99)
# If % 2 is 0, the number is even.
    if num % 2 == 0:
        print('{} is even'.format(num))
    else:
        print('{} is odd'.format(num))

start -= 1

# Since 0 is falsey, we have to invert it with not.
return not num % 2
even.py
import random

start = 5

def even_odd(num):
    while True:

        num = random.randint(1,99)
    # If % 2 is 0, the number is even.
        if num % 2 == 0:
            print('{} is even'.format(num))
        else:
            print('{} is odd'.format(num))

    start -= 1

    # Since 0 is falsey, we have to invert it with not.
    return not num % 2 

1 Answer

Hi there,

You want your while loop to be outside the even_odd method. Write it after the method.

The while loop runs until start becomes zero. At each loop you're generating a random number and passing it into the even_odd method which returns a true/false answer. You then output the result of that with the strings set in the challenge.

So, without any indenting, after the even_odd method, start the while loop. Generate a random number, like you have done, and store it in something; num is fine.

Then start an if statement passing num to even_odd as a parameter. Then output the even string, else output the odd string. Lastly, decrement start else we'll be here all night!

That all looks like:

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

I hope that helps.

Steve.

Thanks Steve!!!