Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Pick a Number! Any Number! The Solution

Python - Can't Start 'Pick a Number' Challenge

I'm having trouble even getting started with this challenge due to an error message.

I practiced 'import random' in the previous videos, but now when I use that line in my code and then test 'random.randint(1, 10)' I get an error message saying that random is undefined. What's going on?

Actually, I believe the 'undefined' error came when I later typed 'import random' in the console. This is what I'm getting when I try to run my code with the 'input random' (line 1) and 'random.randint(1, 10)' (line 3) lines in it:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/random.py", line 3, in <module>
rand_num = random.randint(1, 10)
AttributeError: 'module' object has no attribute 'randint'

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You have a file named random.py. When you do from random import <blah> or import random, Python sees that file and imports it.

input random

is wrong.. please review the video, it should be import random.

Weird I cant access that objective anymore

Shez Azr, I have been typing 'import random'; I misstyped above.

Kenneth, is this related to the cache in Workspaces, or does the 'random.py' file need to be completely deleted? I created a new file titled 'game.py' and trying that still didn't work, giving me the same error message and showing the file it referenced as '/home/treehouse/workspace/random.py'. However, when I created a completely new Workspace, it worked. Thanks for your help!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Python compiles .py files into byte code and stores them as .pyc files. They can both be imported, so make sure you've removed both of them. In Python 3, it stores these .pyc files in the __pycache__ directory, so you'll need to delete them from there (or just delete the whole directory).