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

ayesha mirza
ayesha mirza
1,674 Points

even/odd loop problem


import random

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==False:
    am=andom.randint(1, 99)
    if am%2==0:
        print("{} is even").format(am)
        print("{} is odd").format(am)

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi Ayesha! First let me say that I see that you have a duplicate question here, so I'm going to remove the copy. Also, I see that you've posted around 11 questions in the space of an hour. I feel like it would benefit you to slow down and try to absorb the material you're learning. Also, 11 questions in the space of an hour means that you give other students around 5 and a half minutes to answer before posting a new one. A thoughtful answer with an explanation takes some time to construct. And I promise there are many of us on here who love doing just that.

As for this particular challenge, there are a few problems with your code. First, you've misspelled "random" when trying to generate a random integer. Secondly, the while loop is supposed to run while start is "truthy". But you have it running while start is equal to False. Because start has an initial value of 5 which is a "truthy" value (note that it's not explicitly equal to True), this loop will never run at all. Third, you are supposed to be using the even_odd function to determine if your randomly generated number is even or odd. However, your code never calls that function.

While the check for the remainder when divided by 2 being equal to 0 will result in the same results, it does not meet the requirements of the challenge. Here's how I did it (with explanations):

import random

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

start = 5

while start:  #while start is truthy... start will become falsey when it reaches 0
    number = random.randint(1, 99). #generate a random integer and assign it to the number variable
    if even_odd(number):  #send the integer to the even_odd function to determine if it was even or odd
        print("{} is even".format(number)). #if the value returned from even_odd is true print this
    else:  #otherwise
        print("{} is odd".format(number)). #print this if the value returned was false
    start -= 1 #decrement the start variable

I hope this helps! But remember that patience is a great virtue for a programmer to have! :sparkles: