2. Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
4/7/2019CS151SP19
3. Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
4/7/2019CS151SP19
4. Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
4/7/2019CS151SP19
5. Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
4/7/2019CS151SP19
6. Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
7. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
Instantiate a 'Die' with the Constructor
8. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
Instantiate a 'Die'
myDie = Die()
9. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
10. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
11. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
12. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
13. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
14. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
15. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
16. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
print('Median value:', statistics.median(myRolls))
17. Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
print('Median value:', statistics.median(myRolls))
print('Sigma value:', statistics.stdev(myRolls))
18. Code
<---snip --- >
print('Length of List:', len(myRolls))
print('Median value:',
statistics.median(myRolls))
print('Sigma value:', statistics.stdev(myRolls))
4/7/2019CS151SP19
Rolling die: 1 5
Rolling die: 2 1
Rolling die: 3 7
.
.
.
Rolling die: 9999 5
Length of List: 10000
Median value: 4.0
Sigma value: 1.704
19. Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
What if we were giant nerds and needed a 10 sided
die?
20. Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
What if we were giant nerds and needed a 10 sided
die?
What changes to the code need to be made?
21. Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
• What if we were giant nerds and needed a 10 sided die?
• What changes to the code need to be made?
• We change the 6 to a 10!