File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111
1212class Dice ():
1313 """
14- Example of class with command actions .
14+ Example of dice class .
1515 """
16+ faces = 6
17+
18+
1619 def __init__ (self ):
1720 random .seed ()
21+ self .rollsMade = 0
1822
1923
2024 def roll (self ):
2125 """
22- Move the turtle forward by the specified distance: FORWARD 10
26+ Roll a dice once and return the value.
27+ """
28+ self .rollsMade += 1
29+ return random .randint (1 , self .faces )
30+
31+
32+ def getRollsMade (self ):
33+ """
34+ Get number of rolls made.
2335 """
24- return random . randint ( 1 , 6 )
36+ return self . rollsMade
Original file line number Diff line number Diff line change @@ -23,27 +23,36 @@ class Shell(cmd.Cmd):
2323 prompt = '(dice) '
2424
2525
26+ def __init__ (self ):
27+ super (Shell , self ).__init__ ()
28+ self .dice = dice .Dice ()
29+
30+
2631 def do_roll (self , arg ):
2732 """
2833 Roll one dice one time.
2934 """
30- myDice = dice .Dice ()
31- print ("Rolling a... {}" .format (myDice .roll ()))
35+ print ("Rolling a... {}" .format (self .dice .roll ()))
3236
3337
3438 def do_rolls (self , arg ):
3539 """
3640 Roll one dice several times, supply argument how many times to roll.
3741 """
38- myDice = dice .Dice ()
39-
4042 if not arg :
4143 print ("Missing argument on how many times to roll the dice." )
4244 return
4345
4446 times = int (arg )
4547 for _ in range (times ):
46- print ("Rolling a... {}" .format (myDice .roll ()))
48+ print ("Rolling a... {}" .format (self .dice .roll ()))
49+
50+
51+ def do_total (self , arg ):
52+ """
53+ How many rolls have been made in total?
54+ """
55+ print ("You have rolled the dice {} times." .format (self .dice .getRollsMade ()))
4756
4857
4958 def do_exit (self , arg ):
You can’t perform that action at this time.
0 commit comments