Trying to make a simple random number generator

Viewed 82

I'm super new to python and I'm just trying to make a very simple random number generator function, but nothing seems to be working, and 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 Answers

although random comes with 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 : Python code in one module gains access to the code in another module by the process of importing it.

Related