-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
62 lines (43 loc) · 1.28 KB
/
classes.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
import random
##### Fruit Classes #####
class Fruit():
def __init__(self):
self.flavour, self.colour = random.choice(self.varieties)
def __repr__(self):
return f"<{self.flavour}, {self.colour}, {self.__class__.__name__}>"
class Apple(Fruit):
varieties = [("sour", "green"), ("sweet", "red")]
class Pear(Fruit):
varieties = [("mellow", "yellow"), ("sharp", "green")]
##### Tree Classes #####
class Tree():
def __init__(self):
self.fruits = []
def __repr__(self):
return f"{self.fruit_type.__name__} tree"
def blossom(self):
for i in range(self.fecundity):
self.fruits.append(self.fruit_type())
def harvest(self):
crop = self.fruits
self.fruits = []
return crop
class AppleTree(Tree):
fecundity = 8
fruit_type = Apple
class PearTree(Tree):
fecundity = 5
fruit_type = Pear
##### Cider Classes #####
class Cider():
def __init__(self, fruitlist):
self.flavour = {
"sweet": 0,
"sour": 0,
"mellow": 0,
"sharp": 0
}
for fruit in fruitlist:
self.flavour[fruit.flavour] += 1
def __repr__(self):
return f"A barrel of {max(self.flavour, key = lambda key: self.flavour[key])} cider"