Oct. 01, 2012

Python Game : Rolling the dice

Time again for a game script.

How it works

This is a classic "roll the dice" program. 

We will be using the random module for this,since we want to randomize the numberswe get from the dice. 

We set two variables (min and max) , lowest and highest number of the dice. 

We then use a while loop, so that the user can roll the dice again. 

The roll_again can be set to any value, but here it's set to "yes" or "y", 
but you can also add other variations to it. 

Rolling the dice

import random
min = 1
max = 6

roll_again = "yes"

while roll_again == "yes" or roll_again == "y":
    print "Rolling the dices..."
    print "The values are...."
    print random.randint(min, max)
    print random.randint(min, max)

    roll_again = raw_input("Roll the dices again?")

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.