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

Trevor Byrom
Trevor Byrom
720 Points

Make a while loop that runs until start is falsey.

What am I doing wrong in my code? Everything is fine until I submit step 3 when it says task 1 fails. Pointers please?

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

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

There is a couple of mistakes:

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:  # by saying 'start' python knows it means 'as long as start == True'
    ran_num = random.randint(1, 99)
    if even_odd(ran_num):  # even_odd is a function
        print("{} is even".format(ran_num))
    else:
        print("{} is odd".format(ran_num))
    start -= 1
Trevor Byrom
Trevor Byrom
720 Points

Perfect. Thank you so much Henrik!

Steven Parker
Steven Parker
231,236 Points

Good catch! I didn't notice the function error because the other code actually passed the challenge. :open_mouth:

Clearly it should not have. You might want to report it as a bug to Support.

And while your example shows that you can check if start is "truthy" just by naming it, since it is a number it will never actually be equal to True.

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

since it is a number it will never actually be equal to True. - I've always thought that is was True as long it was > 0? :-p

EDIT: Just sent a mail to support about this bug.

Moderator edit: The problem is that you can pass the challenge by saying:

if even_odd != 0: rest of the code here

but that shouldn't be possible since even_odd is a function.

Edited to include information from help ticket JP

Steven Parker
Steven Parker
231,236 Points

There's a difference between True and "truthy". Try this:

start = 5
if start:
    print('start is "truthy"...')
if start == True:
    print('and start is equal to True.')
else:
    print('but start is NOT equal to True.')
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Wow, took me awhile to find the bug in the challenge. Turns out I was correctly testing the output...but only the last line. So if your last line was correct, the challenge would pass (this is why the if even_odd != 0: was passing sometimes).