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

Task one is no longer passing, but what line have I gone wrong in?

My problem with this has simply become that I don't know which line or lines I've gotten wrong, and therefore I keep trying this or that, but it doesn't pass (which is fine), but now I'm starting to doubt myself

even.py
import random

start = 5

def even_odd(num):
    while start:
        num = random.randint(1,99)
        if num % 2 = 0:
            print("{} is even".format(num))
        elif num % 2 != 0:
            print("{} is odd".format(num))
        start -= 1
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2

5 Answers

jonlunsford
jonlunsford
16,746 Points

Dan: Stuart is correct. The while loop should be outside of the even_odd() function. Even_odd() is provided for you and works as is. It accepts a number and returns True if the number is even, and False if the number is odd. For the challenge you have most of the work. The only change needed is in your if....else statement. You need to pass the random number to even_odd() and if it returns True print the "{} is even" part, and if it returns False print the "{} is odd" part. You are so close to having this pass. Good luck!

PS: make sure when you are testing for equality you use "==" instead of "="

Hi there, my understanding of the challenge is that the while loop should be outside of the odd_even function.

jonlunsford

Thank you. How do I know when a loop needs to be inside or outside of a function?

Hi Dan, it's not that well worded, however, the line below in the challenge is basically saying 'call' the function to get an answer.

If that random number is even (use even_odd to find out)

jonlunsford Okay, I appreciate all the help. I passed it, but I need to go back and understand why.