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 (Retired) Pick a Number! Any Number! Imports

Calling a random num?

It says that I didn't create a "random_num" function but I think I did. Whats up here?

random_num.py
import random

def random_num(num):
  y = randint(1,num)
  return y
jack hamilton
jack hamilton
5,609 Points

I just attempted it as well not working for either.

2 Answers

Andrew K
Andrew K
13,774 Points

Hi Ben, You either need to specify what you are importing from the "random" module initially (from random import randint), or else specify the module when calling the randint function. So:

import random

def random_num(num):
  y = random.randint(1,num)
  return y

That should do it! (Worked for my test at least)

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

Hey Ben

When you import a module, you need to use the module's name to access any of its methods. For example, if you have a module named math and you want to call the add method, you would need code that looks something like this:

import math

def num_plus_two(num):
    return math.add(2, num)