Dice Roll Program in Python

We can write a simple dice rolling program in python using the “random” module and the if statement.


Step 1: In step 1, We import the random module by giving the command import random and print the statements that will be visible to the user i.e.

print (“This is a Dice Rolling Program”)
    print (“Press Enter to roll”)

After we print the statements, we then write the input() after we write Press Enter to roll then the program rolls the dice and continues with the program else the dice will roll on its own the input() function tells it to roll after we press Enter. Then we roll the dice randomly to obtain the result and store it in a variable called number. we roll the dice with :

number = random.randint(1,6)


Step 2: Then we have to show what is the outcome of the dice roll. For that we have to write the if statement i.e. :

if number==1:
        print (“[———-]”)
        print (“[               ]”)
        print (“[      O      ]”)
        print (“[               ]”)
        print (“[———-]”)

As we have stored the random number in the number variable between 1 and 6 , we can now check for each number that can be the possibility and then we can show that side of the Dice. From the above example we can create that for all the numbers from 1 to 6.


Step 3: Here we see that the dice rolls just once if we run the above program with the stated code. So, if we want to make it roll a few times then we have to write that the whole code in a while loop which if returns true will enter the block else will exit. So we select the whole code and then give the indentation then write the while loop a follows:

quit = 0
while quit != “q”:

Here we define a variable quit and assign it to 0.Then we give the looping condition that if the value of quit is not equal to the character ‘q’ then continue the dice roll else not.


Step 4: Now we have to ask the user after each dice roll if he wants to quit, for that we write the code below the code in step 2 as the below example:

if number==1:
        print (“[————]”)
        print (“[                  ]”)
        print (“[        O       ]”)
        print (“[                 ]”)
        print (“[———–]”)
        print (“Type q to quit”)

As we know the use of the input() function, it stores the value that the user inputs and saves it in quit. So that, the while loop will know where to stop rolling the Die.


This is the final output of your Program. As it encountered a q it stopped rolling the die.

Leave A Reply

Your email address will not be published. Required fields are marked *