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

issue on 3rd part of challenge, not sure how to decrement start, also not 100 if my code is correct

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

Make a while loop that runs until start is falsey.

Inside the loop, use random.randint(1, 99) to get a random number between 1 and 99.

If that random number is even (use even_odd to find out), print "{} is even", putting the random number in the hole. Otherwise, print "{} is odd", again using the random number.

Finally, decrement start by 1.

I know it's a lot, but I know you can do it!

import random

start = 5

def even_odd(n_num):
    while start == True:
        n_num = random.randint(1,99)
        new_num = n_num % 2
        if new_num != 0:
            print("{} is odd".format(n_num))
        else:
            print("{} is even".format(n_num))
                #return not % 2 ----- not sure what to do with this pre-inserted code
    else:
        start -1

#while loop                                 >>>
#make a random int call 1-99                >>>
#print if number is even/even_odd           >>>
#decrement start by 1

even_odd(n_num)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The intent of the challenge is to used the function even_odd in a while loop:

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

# Make a while loop that runs until start is falsey.
while start:
    # Inside the loop, use random.randint(1, 99) to get a random number between 1 and 99.
    n_num = random.randint(1,99)
    if even_odd(n_num):
        # If that random number is even (use even_odd to find out)...
        # print "{} is even", putting the random number in the hole. 
        print("{} is even".format(n_num))
    else:
        # Otherwise, print "{} is odd", again using the random number.
        print("{} is odd".format(n_num))
    # Finally, decrement start by 1.
    start -= 1

I understand now what it was asking and I thank you for your help, however when I run this code in the challenge it fails, when I run the same thing in my terminal it is perfect, there are no variations in either script. Did you run this in the challenge?

Hi Gerald,

Chris' code is just missing the amount to decrement start by at the very end. If you enter 1, then his code will pass, like so;

import random

start = 5

def even_odd(num):
    return not num % 2

while start:
    n_num = random.randint(1,99)
    if even_odd(n_num):
        print("{} is even".format(n_num))
    else:
        print("{} is odd".format(n_num))
    start -= 1
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Sorry. Cut-and-paste error missed the final 1. It was run in the challenge and passed. Thanks Evan for catching this.

I figured that part out actually, my issue was something a bit more obvious than that... apparently I indented the while block, I am an idiot. Thank you both Chris and Evan for your help!