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

Ryan Walton
Ryan Walton
1,298 Points

Even and odd Task 3/3 Help!! (Task 1 is no longer working)

I do not know where i am going wrong with the code.

even.py
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_number > 0:
    random_num = random.randint(1, 99)
    answer = even_odd(random_num)
    if answer == 0:
        print("{} is even".format(answer))
        start -= 1
        sys.exit()
    else:
        print("{} is odd".format(answer))
        start -= 1
        sys.exit()
Ryan Walton
Ryan Walton
1,298 Points

i fixed up the naming of "start_number" to "start", didn't seem to do the trick

1 Answer

Couple of things...

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

# in the condition you probably meant 'start' not start_number
#while start_number > 0:
while start > 0:
    random_num = random.randint(1, 99)
    answer = even_odd(random_num)
    #even_odd is  returning not of 0, so its one, i'd rather suggest to check it with true or false. something like [if answer==True:]
    if answer == 1:
        #its the random number that is to be proved as even or else otherwise
        print("{} is even".format(random_num))
        #start -= 1
        #we shouldn't use exit() function, because it terminates the program, rather just break the loop
        #sys.exit()
        #you dont have to 'break' it either. The loop is suppose to run 5 times
        #break
    else:
        #its the random number that is to be proved as even or else otherwise
        print("{} is odd".format(random_num))
        #start -= 1
        #sys.exit()
        #break
    #if we are doing something in both the conditions we can rather specify it outside the loop itself(although is not the problem, just gives a better look)
    start -= 1