Skip to content

Commit 5145ae0

Browse files
Merge pull request geekcomputers#175 from Angus3321/patch-2
Add some judgement when changing sides.
2 parents 361f08e + ef81a1d commit 5145ae0

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

dice.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@
1010

1111
import random
1212
class Die(object):
13-
def __init__(self, sides):
14-
self.sides = sides
13+
#A dice has a feature of number about how many sides it has when it's established,like 6.
14+
def __init__(self):
15+
self.sides=6
16+
17+
"""because a dice contains at least 4 planes.
18+
So use this method to give it a judgement when you need to change the instance attributes."""
19+
def set_sides(self,sides_change):
20+
if self.sides_change>=4:
21+
self.sides=self.sides_change
22+
print("change sides!")
23+
else:
24+
print("wrong sides!")
25+
1526
def roll(self):
1627
return random.randint(1, self.sides)
1728

18-
d = Die(6)
19-
d1 = Die(6)
29+
d = Die()
30+
d1 = Die()
31+
d.set_sides(4)
32+
d1.set_sides(4)
2033
print (d.roll(), d1.roll())

0 commit comments

Comments
 (0)