Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

I am new to python and I am just trying to do simple random number generator function but python just gives me an error every time. Could anyone help with this?

def roll_dice(num):

    return random.randint(1, num)

roll_dice(6)

Error Message:

NameError: name 'random' is not defined

1 Answer

0 votes
by (36.8k points)
edited by

Although random comes with the python itself (you don't need to install it)

But to use it you need to import it

import random # you need to import random

def roll_dice(num):

    return random.randint(1, num)

roll_dice(6)

Import means: The Python code in one of the module gains access to the code in another module by the process of importing it.

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...