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

Kevin Lankford
Kevin Lankford
1,983 Points

"task 1 is no longer passing"

Not sure why I'm getting this error. I tested it on the console and it works. What is not passing?

even.py
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
Kevin Lankford
Kevin Lankford
1,983 Points

Didn't look like my code showed up. Here it is

while start != 0:
    rand = random.randomint(1, 99)
    if even_odd(rand):
        print("{} is even".format(rand))
    else:
        print("{} is odd".format(rand))

    start -= 1

2 Answers

Hi there Kevin, there are just a couple of minor mishaps in your code that you should fix. Firstly, it doesn't look like you have imported random neither have you defined start. However, the key error here is your have typed random.randomint which actually does not exist. The name of the function is is actually randint instead. Fix this and you should be good to go!

import random

def even_odd(num):
    return not num % 2

start = 5

while start != 0:
    rand = random.randint(1, 99)   #here you have written random.randomint
    if even_odd(rand):
        print("{} is even".format(rand))
    else:
        print("{} is odd".format(rand))

    start -= 1

If you have any further questions please feel free to leave a comment :)

Thanks,

Haider

Kevin Lankford
Kevin Lankford
1,983 Points

My apologies this was code that I had copied somewhere but fixed to posting this. I still seem to be running into this "task 1" issue. Here is my code:

import random

start = 5

while start != 0:
    rand = random.randint(1, 99)
    if even_odd(rand):
        print("{} is even".format(rand))
    else:
        print("{} is odd".format(rand))

    start -= 1

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

Hi again Kevin, the reason why you are running into this error is because you are actually calling your even_odd function before you have defined it. Copy the function and paste it before the loop and you should be good to go. This is because python carries out code in the order it is listed and your even_odd function is defined after it is called. :smile: