Simulate Dice in Python
This challenge has been written by Marko Šolić and is part of the EU CODE WEEK CHALLENGES.
Purpose of the challenge
- The purpose of this challenge is to introduce students to random number generation and loops in Python.
- By simulating a dice roll, students will learn how computers can generate random outcomes and how to repeat actions multiple times using a loop.
- This exercise also lays the foundation for building simple games and simulations.
Duration
30 to 45 minutes for a basic version
Up to 60 minutes if exploring advanced options (two dice, most frequent roll, etc.)
Experience
No prior programming experience is required. Basic familiarity with typing and using a computer is enough.
Target audience
Primary and secondary school students, beginners in Python, anyone learning about random numbers, games, or basic programming logic.
Recommended tool
- To get started, you need to have Python installed on your computer. If you haven't already, download Python from the official website: https://www.python.org
- Open Python IDLE or any text editor (e.g. Visual Studio Code, PyCharm) to write your code.
Additional resources:
- Python official documentation - random module
- Learn Python https://docs.python.org/3/library/random.html
Simulate Dice in Python
Primary and secondary school students, beginners in Python, anyone learning about random numbers, games, or basic programming logic.
Description of the challenge
Dice-roll simulation is a great way to learn basic work with random numbers in Python.
In this lesson, we will learn how to use the random module to generate random numbers and how to use that number to simulate a roll of the dice.
Through this task, you will understand how Python can generate numbers within a certain range.
Instructions
Step 1: Preparing the environmentTo get started, you need to have Python installed on your computer. If you haven't already, download Python from the official website. Open Python IDLE or any text editor (e.g. Visual Studio Code, PyCharm) to write code.
Step 2: Writing the programme- Open Python IDLE and create a new file to call dice.py.
- Enter the following code:
#We import a 'random' module for generating random numbers
We print a greeting to the user
print("Welcome to the roll-dice simulation!")
import random
Ask the user how many times he wants to roll the dice
throw_num = int(input("How many times do you want to roll the dice? "))
for i in range(throw_num):
result = random.randint(1, 6) #We generate a random number between 1 and 6
print(f"Throwing {i + 1}: {result}")
print("Thanks for playing!")
Visual representation of the code in Python IDLE
Make sure that the indentations in your code are the same like in the example above!
Step 3: Explanation of the code- Import random module: The random module allows you to generate random numbers. The random.randint(a, b) function returns a random integer in the range from a to b, including both ends.
- Enter the number of throws: The programme asks the user to enter the number of times he wants to roll the dice. We convert the entered number to an integer using int().
- Toss simulation: The programme uses a loop to perform a dice simulation as many times as the user has entered. For each throw, a random number between 1 and 6 is generated, representing the result of the throw.
- Print results: For each throw, the result is printed (the number that has "fallen" on the dice). At the end of the programme, a message of gratitude is printed
- Save the file as dice.py.
- Run your programme at Python IDLE.
- Enter the number of times you want to roll the dice (e.g. 5).
- The programme will generate random numbers and print them as a result of each throw.
1. Which module in Python allows you to generate random numbers?
- random
- Math
- Time
2. What does the random.randint(1, 6) function do?
- Generates a random number between 1 and 6
- Prints a random number on the screen
- Prints a number between 1 and 6
3. How do you convert a user entry to an integer in Python?
- float(s)
- int()
- p()
Correct answers: 1.a, 2.a, 3.b
Modify programme:
- Add an option to simulate two dice throws and add their results together.
- Add an option for the programme to end up printing the number of times that the programme has "dropped" on the dice the most.
Share the link to your work in your Instagram Bio: tap the Edit Profile button on your Instagram and add the link to your work in the Website field. After that, create a new post, add a screenshot of your work, write “Link in Bio”, add the hashtag #EUCodeWeekChallenge and mention @CodeWeekEU.