-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyDie.py
More file actions
51 lines (37 loc) · 1.15 KB
/
MyDie.py
File metadata and controls
51 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# MyDie.py
from random import randrange
class MyDie:
def __init__(self, sides): #constructor
self.sides = sides
self.value = 1
def roll(self): #mutator method
self.value = randrange(1,self.sides+1)
def getValue(self): #accessor method
return self.value
def setValue(self, value): #mutator method
self.value = value
def main():
numsides = getInteger("Enter the number of sides for the die: ")
#Construct an MyDie object
die1 = MyDie(numsides)
numrolls = getInteger("Enter the number of rolls: ")
displayRolls(die1,numrolls)
def getInteger(msg):
myinput = ""
while myinput=="":
try:
myinput = int(input(msg))
except ValueError:
print("Not an integer.")
return myinput
def displayRolls(dieOne,numrolls):
for i in range(numrolls):
#Roll the die object
rollDie(dieOne)
#Display the value of the die
displayDie(dieOne)
print()
def rollDie(dieA):
dieA.roll()
def displayDie(dieB):
print(dieB.getValue(), end=" ")