Skip to content

Commit 261af24

Browse files
authored
Update dice_rolling_game.py
1 parent 1a38102 commit 261af24

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed
Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,84 @@
11
# Simple Dice Rolling game
2-
import random # random library for generating random numbers
2+
import random
33

4-
def roll_dice(): # A function to roll the dice
5-
while True: # while playing the game
4+
def create_dice():
5+
dice_faces = {
6+
1:{ """
7+
+-------+
8+
| 1 |
9+
| ● |
10+
| |
11+
+-------+
12+
"""},
13+
2:{ """
14+
+-------+
15+
| ● |
16+
| 2 |
17+
| ● |
18+
+-------+
19+
"""},
20+
3:{ """
21+
+-------+
22+
| ● 3 |
23+
| ● |
24+
| ● |
25+
+-------+
26+
"""},
27+
4:{ """
28+
+-------+
29+
| ● ● |
30+
| 4 |
31+
| ● ● |
32+
+-------+
33+
"""},
34+
5: {"""
35+
+-------+
36+
| ● 5 ● |
37+
| ● |
38+
| ● ● |
39+
+-------+
40+
"""},
41+
6:{ """
42+
+-------+
43+
| ● ● |
44+
| ● 6 ● |
45+
| ● ● |
46+
+-------+
47+
"""}
48+
}
49+
return dice_faces
650

7-
user_input = input("Roll the dice? (y/n): ").lower() # Accepting user inputs in lower case
51+
def roll_dice(dice_faces):
852

9-
if user_input == "y": # Continue if user_input is y
53+
options = ['y', 'n']
1054

11-
die1 = random.randint(1,6) # variables for dice
12-
die2 = random.randint(1,6)
55+
while True:
56+
roll = input('Roll the dice (y/n): ')
1357

14-
print(f"You rolled a {die1} and a {die2}")
58+
if roll not in options:
59+
print('Enter a valid input (y/n) \n')
60+
61+
else:
1562

16-
elif user_input == "n": # Break from the loop if user input is n
17-
print("Thanks for playing...")
18-
break
63+
if roll == 'n':
64+
print('Thanks for playing...😁👌')
65+
break
1966

20-
else: # Check if user input is invalid
21-
print("Invalid input")
67+
else:
2268

23-
roll_dice()
69+
dice1 = random.randint(1,6)
70+
dice2 = random.randint(1,6)
71+
72+
print(f'Dice rolled: {dice1} and {dice2}')
73+
print('\n'.join(dice_faces[dice1]))
74+
print('\n'.join(dice_faces[dice2]))
75+
76+
77+
78+
79+
80+
def main():
81+
dice_faces = create_dice()
82+
roll_dice(dice_faces)
83+
84+
main()

0 commit comments

Comments
 (0)