انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

Project: is the dice fair?

Section 11: Randomness and errorsLesson 11.2 · ⏱ 10 min
🎯 Project goal
We roll the dice 6000 times, count every face in a dictionary, and draw a bar of # for each face.

Predict before running: if the dice is fair, about how many times does each face appear?

import random

counts = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
for i in range(6000):
    face = random.randint(1, 6)
    counts[face] += 1

for face in counts:
    print(face, counts[face], "#" * (counts[face] // 50))
print("total:", sum(counts.values()))
🖥️ The output in Robo Python:
1 1039 ####################
2 1020 ####################
3 1001 ####################
4 968 ###################
5 953 ###################
6 1019 ####################
total: 6000
🐍 Open the code in Robo Python ↗
Dice statistics: the bars are close
Dice statistics: the bars are close
🖥️ What happens on the stage?
The numbers change with every run, but they stay close to 1000 for each face. That is what a fair dice means.
  1. Roll two dice together and add them, and count the results from 2 to 12. Which sum appears most? And why?

📌 Another example: a coin: heads or tails?

We toss the coin 1000 times and count.

🐍 A coin: heads or tails?
import random
heads = 0
for i in range(1000):
    if random.choice(["heads", "tails"]) == "heads":
        heads = heads + 1
print("heads:", heads)
print("tails:", 1000 - heads)
🖥️ The output in Robo Python:
heads: 496
tails: 504
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Roll the dice 10 times and print the results in a list.

👀 Show the solution
🐍 Solution
import random
rolls = []
for i in range(10):
    rolls.append(random.randint(1, 6))
print(rolls)
🖥️ The output in Robo Python:
[6, 4, 4, 6, 3, 4, 6, 3, 3, 1]
🐍 Open the code in Robo Python ↗

Result: a list of 10 numbers between 1 and 6.

Medium

How many times do we roll the dice until a 6 appears?

👀 Show the solution
🐍 Solution
import random
count = 0
while True:
    count = count + 1
    if random.randint(1, 6) == 6:
        break
print("6 after", count, "rolls")
🖥️ The output in Robo Python:
6 after 1 rolls
🐍 Open the code in Robo Python ↗

Result: a different number every time.

Hard

Two dice together 1000 times: how many times was the sum 7?

👀 Show the solution
🐍 Solution
import random
sevens = 0
for i in range(1000):
    if random.randint(1, 6) + random.randint(1, 6) == 7:
        sevens = sevens + 1
print("sevens:", sevens)
🖥️ The output in Robo Python:
sevens: 180
🐍 Open the code in Robo Python ↗

Result: about 167 times: seven is the sum that comes up most.

Finished the lesson? Mark it so your progress counts.