Coding a dice on Python

Coding a dice on Python where the input is a random number only requires a few lines of code. Three to be exact. Below you will find the code that will give you a random number between 1 and 6.

 
import random 
roll_dice = random.randint(1, 6)
print(roll_dice)
 

Anatomy of the code

 
 
Python Library

The steps are straightforward:

Find the appropriate book(aka module) in the Python library. In this case random.

Use the import statement to take it out of the bookcase(aka library).

Random Randint python library sabina schmitz

In the book (aka: module) of random, find the recipe (aka: function) on randint.

Randint function visualized

This is the recipe (aka: function):

random.randint(a,b)
randint function visualized

Hmm, this describes the exact characteristic of a dice. Thus, we shall call this recipe (aka: function) roll_dice. As a dice only has 6 sides from 1 to 6, the ranges of a & b are given.

roll_dice = random.randint(1,6)

Print the recipe(aka:function) with the print() function

 
 
magician sabina schmitz
random gif python