8000 Update solutions to 8.7 · rakash/python-basics-exercises@197627e · GitHub
[go: up one dir, main page]

Skip to content

Commit 197627e

Browse files
committed
Update solutions to 8.7
1 parent f8ea93e commit 197627e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77

88
# Exercise 1
9-
# Simulate the toss of a die
10-
print(randint(1, 6))
9+
# Write a function that simulatee the roll of a die.
10+
def roll():
11+
"""Return random integer between 1 and 6"""
12+
return randint(1, 6)
1113

1214

1315
# Exercise 2
14-
# Simulate 10,000 throws of dice and display the average result
16+
# Simulate 10,000 rolls of a die and display the average number rolled.
1517
trials = 10000
1618
total = 0
19+
1720
for trial in range(0, trials):
18-
total += randint(1, 6)
19-
avg_result = total / trials
20-
print(f"The average result of {trials} throws was {avg_result}")
21+
total = total + roll()
22+
23+
avg_roll = total / trials
24+
25+
print(f"The average result of {trials} throws is {avg_roll}")

0 commit comments

Comments
 (0)
0