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

Ferdinand Pretorius
Ferdinand Pretorius
18,705 Points

Need help with python challenge (Treehouse is drunk would be more accurate)

Apparently I'm doing something wrong on task 3


When i run my code locally in gnome shell using python 2 and 3 it executes without any problems, gives 5 random numbers and then the while loop becomes falsey and it quits, as expected!

Still when i do the challenge i keep getting the error: Oops! It looks like Task 1 is no longer passing...

Here is the description!

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.
If that random number is even (use even_odd to findout), 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!

And here is my code!

even.py
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:
    rnum = random.randint(1, 99)

    if even_odd(rnum):
        print "{} is even".format(rnum)
    else:
        print "{} is odd".format(rnum)
    start -= 1

2 Answers

Not sure what is wrong in your code this worked for me

import random
def even_odd(num):
    return not num % 2

start = 5
while start:
    num = random.randint(1, 99)

    if even_odd(num):
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))
    start -= 1

Python 3 has a new print function...but i am not sure if that was the problem

Ferdinand Pretorius
Ferdinand Pretorius
18,705 Points

I didn't even think to use the print function, i thought PEP 3105 print statement would still be valid in the test, turns out i was wrong.

Cheers for info, this worked.

Hanifah W
Hanifah W
395 Points

Why the variable 'start'. Not sure what it does here? Wouldl appreciate any clarity someone can give,

John Judge
John Judge
3,527 Points

Hanifah W I believe start is acting as a countdown. It 'starts' at 5, and each time the while loop iterates decrements by 1. Once it hits 0 it becomes falsey and breaks the loop.

You need "start" to act as a countdown from 5.