-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit2_ex2.4.2.py
69 lines (53 loc) · 1.78 KB
/
unit2_ex2.4.2.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
# exercise 2.4.2 from unit 2
'''
Exercise 2.4.2
Define a class called BigThing which receives as a parameter during creation a variable of a certain type (can be anything: string, list, number, etc.).
Add a method called size to the class that works as follows:
If the variable is a number - the method returns the number
If the variable is a list/dictionary/string - the method returns the length of the variable (len).
Below is a sample run:
my_thing = BigThing("balloon")
print(my_thing.size())
7
Define another class called BigCat which inherits from the BigThing class and also receives a weight during creation:
If the weight is greater than 15, the size method will return "Fat"
If the weight is greater than 20, the size method will return "Very Fat"
Otherwise, the method returns OK.
Below is a sample run:
cutie = BigCat("mitzy", 22)
print(cutie.size())
Very Fat
'''
# 1:
class BigThing:
def __init__(self, thing):
self._thing = thing
def size(self):
if isinstance(self._thing, (int, float)):
return self._thing
elif isinstance(self._thing, (list, dict, str)):
return len(self._thing)
# 2:
class BigCat(BigThing):
def __init__(self, name, weight):
self._name = name
self._weight = weight
super().__init__(self._name)
def size(self):
if self._weight > 15 and self._weight <= 20:
return "Fat"
elif self._weight > 20:
return "Very Fat"
else:
return "OK"
def main():
my_thing = BigThing("balloofgggn")
print(my_thing.size())
my_thing = BigThing([1, 2, 3, 4, 5])
print(my_thing.size())
my_thing = BigThing(42)
print(my_thing.size())
cutie = BigCat("mitzy", 22)
print(cutie.size())
if __name__ == '__main__':
main()