name 'random' is not defined [closed] ·
Jessica Young
Published Jan 19, 2026
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 1you need to import the random library with the import keyword
import random random.randint(1,10) #no. between 0 and 10 2ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaWttaWuFc4OOp5imnZWnv7C%2BjKeYpp1dp66vsM6mZKKrXaO8tXnDnp2ippWZ