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 trialcarterberry
7,806 PointsPython - 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?
3 Answers
Kenneth Love
Treehouse Guest TeacherYou have a file named random.py
. When you do from random import <blah>
or import random
, Python sees that file and imports it.
shezazr
8,275 Pointsinput random
is wrong.. please review the video, it should be import random.
Weird I cant access that objective anymore
carterberry
7,806 PointsShez 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
Treehouse Guest TeacherPython 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).
carterberry
7,806 Pointscarterberry
7,806 PointsActually, 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'