Skip to content

Commit 2ac3e1a

Browse files
committed
updates to dice cmd example
1 parent 15a0891 commit 2ac3e1a

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎example/cmd/dice/dice.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@
1111

1212
class 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

‎example/cmd/dice/diceshell.py‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff 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):

0 commit comments

Comments
 (0)