-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-2.py
More file actions
79 lines (65 loc) · 1.87 KB
/
demo-2.py
File metadata and controls
79 lines (65 loc) · 1.87 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 15 22:37:06 2021
@author: Daniel Mishler
"""
# Demo 2: Classes and random numbers
import random
# import is for libraries
# libraries are extra functions that are still part of python!
# you just access them differently.
# print(random.randint(1,2)) # random number between 1 and 2
# Where is Python in this big list of numbers?
# It's at the *Seed*
random.seed(6)
print(random.randint(1,2))
print(random.randint(1,2))
print(random.randint(1,2))
def d6():
return random.randint(1,6)
# Classes
# A class is an object in Python that has data (variables) and methods
class D6:
# every time you make a class, you have to use this function.
# And the first argument has to be "self"
def __init__(self):
return
def roll(self): # Always use "self" as first argument!
return random.randint(1,6)
# Once a class exists, you can make many of that class.
# You store the class in a variable!
myD6 = D6()
print(myD6.roll())
class Die:
def __init__(self,dieMax):
self.max = dieMax
self.name = "d"+str(self.max)
return
def roll(self): # Always use "self" as first argument!
return random.randint(1,self.max)
def loudRoll(self):
result = random.randint(1,self.max)
print(self.name,"rolls",result)
return result
myDie = Die(9)
class Dice:
def __init__(self,dieList):
self.dice = dieList
return
def rollCall(self):
for i in self.dice: # For each die
print(i.name)
def roll(self):
result = 0
for i in self.dice:
result += i.roll()
return result
def loudRoll(self):
result = 0
for i in self.dice:
result += i.loudRoll()
print("group total:",result)
return result
mySecondDie = Die(7)
myList = [myDie,mySecondDie]
myDice = Dice(myList)