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

Richard Farr
Richard Farr
4,857 Points

import random not working and rest not too?

It says

import random and the function does not work but I can't figure out why.

any suggestions?

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

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

your function returns a boolean, but in the while loop you are comparing it to 0. change 0 to true, and also you are missing the colon : after the 0 in the if statement.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You're very close. There is a missing colon:

if oddeven == 0   # <-- missing colon

This causes a syntax error which throws off all the previous task checks. The random import is ok.

The other error is a reversed polarity. even_odd returns True if it's even. So the you'll need to use if oddeven == 1: or, better yet, use the "truthiness" and simply code: if oddeven: