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

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

Sarah Jenkins

Senior Technology Editor & AI Specialist

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.

Share this article
Twitter Facebook Pinterest