Nameerror: Name 'Random' Is Not Defined [Closed]

Nameerror: Name 'Random' Is Not Defined [Closed]

Here is my code:

r = random.randint(1,10)

But for some reason it's giving me the error

NameError: name 'random' is not defined

Other info: Mac, Python, 3.4.0 pylauncher

3 Answers

You have to import the module random:

import random

r = random.randint(1,10)
# ...
0
>>> import random
>>> r = random.randint(1,10)
>>> r
10
1

you need to import the random library with the import keyword

import random 
random.randint(1,10)    #no. between 0 and 10
2
Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.