File tree Expand file tree Collapse file tree 1 file changed +75
-14
lines changed
Python_Begginer_Projects/Easy Expand file tree Collapse file tree 1 file changed +75
-14
lines changed Original file line number Diff line number Diff line change 1
1
# Simple Dice Rolling game
2
- import random # random library for generating random numbers
2
+ import random
3
3
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
6
50
7
- user_input = input ( "Roll the dice? (y/n): " ). lower () # Accepting user inputs in lower case
51
+ def roll_dice ( dice_faces ):
8
52
9
- if user_input == "y" : # Continue if user_input is y
53
+ options = [ 'y' , 'n' ]
10
54
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): ' )
13
57
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 :
15
62
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
19
66
20
- else : # Check if user input is invalid
21
- print ("Invalid input" )
67
+ else :
22
68
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 ()
You can’t perform that action at this time.
0 commit comments