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) Number Game App Even or Odd

NameError: name 'num' is not defined

I can't figure out what's wrong with my code. I keep getting the error message NameError: name 'num' is not defined. Please help.

even.py
import random

even_odd = num
num = random.seed
if num % 2 == 0:
    print("True")
else :
    print("False")
Albert Reel
Albert Reel
5,555 Points

You need to set num before you use it to set even_odd

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You have the right idea. The syntax needs to be corrected:

import random

def even_odd(num):  # <-- correct function definition
    # num = random.seed  # <-- 'num' passed as argument
    if num % 2 == 0:
        return True  # <-- return not print
    else :
        return False  # <-- return not print

Thanks Chris!