Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 106f5dd

Browse files
New project for a Dice Rolling Simulator.
1 parent 6501f90 commit 106f5dd

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Beginner/12_dice_rolling_simulator.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import random
2+
3+
def roll_dice(num_dice):
4+
"""
5+
Simulates rolling a specified number of standard 6-sided dice.
6+
7+
Args:
8+
num_dice (int): The number of dice to roll.
9+
10+
Returns:
11+
tuple: A tuple containing:
12+
- list: A list of the individual dice roll results.
13+
- int: The sum of all dice rolls.
14+
"""
15+
rolls = []
16+
total_sum = 0
17+
for _ in range(num_dice):
18+
# Roll a single 6-sided die
19+
roll = random.randint(1, 6)
20+
rolls.append(roll)
21+
total_sum += roll
22+
return rolls, total_sum
23+
24+
def main():
25+
"""
26+
Main function to run the dice rolling simulator.
27+
"""
28+
print("--- Dice Rolling Simulator ---")
29+
30+
while True:
31+
try:
32+
# 1. Prompt the user for the number of dice to roll
33+
num_dice_str = input("How many dice do you want to roll? ")
34+
num_dice = int(num_dice_str)
35+
36+
if num_dice <= 0:
37+
print("Please enter a positive number of dice.")
38+
continue # Ask again
39+
40+
# 2. Roll the dice
41+
individual_rolls, total = roll_dice(num_dice)
42+
43+
# 3. Display the results
44+
print(f"\nRolling {num_dice} dice...")
45+
print(f"Individual rolls: {individual_rolls}")
46+
print(f"Total sum: {total}")
47+
break # Exit the loop after successful roll
48+
49+
except ValueError:
50+
print("Invalid input. Please enter a whole number.")
51+
except Exception as e:
52+
print(f"An unexpected error occurred: {e}")
53+
break # Exit on other errors
54+
55+
if __name__ == "__main__":
56+
main()

‎README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,40 @@ These projects are ideal for those new to Python. Each project includes a descri
393393

394394
</details>
395395

396+
### 12. Dice Rolling Simulator
397+
- **Description**: Create a dice rolling simulator.
398+
399+
- **Solution**: https://github.com/Infinitode/Python-Projects/blob/main/Beginner/12_dice_rolling_simulator.py
400+
401+
- **Steps**:
402+
1. Prompt the user for the number of dice to roll.
403+
2. Roll the dice.
404+
3. Display the results.
405+
406+
- **Tips:**
407+
408+
</summary>
409+
<details><summary>Tip 1:</summary>
410+
411+
Use `input()` for input.
412+
413+
</details>
414+
<details><summary>Tip 2:</summary>
415+
416+
Use `random.randint()` to generate a random number between 1 and 6.
417+
418+
</details>
419+
<details><summary>Tip 3:</summary>
420+
421+
Use a `loop` to roll multiple dice.
422+
423+
</details>
424+
<details><summary>Tip 4:</summary>
425+
426+
Print out the results using `print()`.
427+
428+
</details>
429+
396430
> [!NOTE]
397431
> Working code solutions are in the `/Beginner` folder.
398432

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /