-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscussion6.py
147 lines (99 loc) · 3.02 KB
/
discussion6.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Skittle:
def __init__(self, color):
self.color = color
class Bag:
number_sold = 0
def __init__(self):
self.skittles = []
Bag.number_sold += 1
def tag_line(self):
print("Taste the rainbow!")
def print_bag(self):
print([s.color for s in self.skittles])
def take_skittle(self):
return self.skittles.pop(0)
def add_skittle(self, s):
self.skittles.append(s)
def take_color(self, color):
for skittle in self.skittles:
if skittle.color == color:
return self.skittles.pop() # pops the current index
return 'No skittle of that color in bag'
def take_all(self):
for skittle in self.skittles:
print(skittle.color)
self.skittles = ()
s1 = Bag()
# adding Skittle objects instances and instance attributes
for color in ['red', 'blue', 'yellow']:
s1.add_skittle(Skittle(color))
ans = s1.take_all()
"""
def take_all(self):
out = [s for s in self.skittles]
for s in self.skittles:
self.skittles.remove(s)
del self.skittles[0]
print([s.color for s in out])
return out
"""
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True # why pass it into the init and not class?
self.name = name
self.owner = owner
def __repr__(self):
return f"{self.name} is {self.owner}'s pet"
def eat(self, thing):
print(f'{self.name} ate a {thing}!')
def talk(self):
print('Woof!')
#####################
#####################
class Cat(Pet):
def __init__(self, name, owner, lives=9):
Pet.__init__(self, name, owner)
self.lives = lives
def talk(self):
print('Meow')
def lose_life(self, num):
self.lives -= num
if self.lives <= 0:
self.is_alive = False
print(f'{self.name} lost all his lives')
return self.lives
def _1up(self):
self.lives += 1
self.is_alive = True
return self.lives
"""mr_waffles = Pet('Mr Waffles', 'Nicol')
print(mr_waffles)
mr_waffles.eat('cookie')
mr_waffles.talk()"""
tor = Cat('Thor', 'Jona')
tor.talk()
test = tor.is_alive
tor.lose_life(9)
tor._1up()
test = tor.is_alive
##################
#################
class Cat(Pet): # no need to repeat the __init__
lives = 9
"""
def __init__(self, name, owner, lives=9):
Pet.__init__(self, name, owner)
self.lives = lives"""
def talk(self):
print('Meow')
def lose_life(self, num):
self.lives -= num
if self.lives <= 0:
self.is_alive = False
print(f'{self.name} lost all his lives')
return self.lives
def _1up(self):
self.lives += 1
self.is_alive = True
print('back from the dead!')
return self.lives