Newer
Older
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
# %% Imports
from bed import Bed
from fruit import Fruit
from plant import FruitBearingPlant, RootVegetable
# from root import Root
# %% Create objects
plants = [FruitBearingPlant("strawberry", 3, Fruit("strawberry", 33)),
FruitBearingPlant("tomato", 4, Fruit("tomato", 16))]
bed = Bed("in the garden")
# Let's call some methods
c = " >" # cursor
# Method: __str__
print(" Bed Methods ".center(79, "-"))
print(c, "Print informal string representation of <bed>\n")
print(bed, end="\n\n")
# Property: needs_water
print(c, "<bed> needs water?\n")
print(bed.needs_water, end='\n\n')
# Method: water
amount = 9
print(c, f"Water <bed> (amount={amount})\n")
bed.water(amount)
print(bed, end="\n\n")
# Method: plant_plant
print(c, f"Plant plants {plants}\n") # For every plant in the list, the formal string repr. is called (__repr__)
for plant in plants:
bed.plant_plant(plant)
print(f"Planting {plant}") # this calls the informal string representation of the plant
print(f"\n{bed}")
# %%
# bed.water(amount - bed.water_level)
# Method: update
periods = amount // len(plants) # this is the operator for a floor division
print(c, f"Update bed for {periods} periods\n")
for _ in range(periods):
bed.update()
print(f"Plants: {[str(plant) for plant in bed.plants]}\n")
print(bed, end="\n\n")
# Property: water_level.setter
print(c, "Update <bed> and check water_level\n")
bed.update()
print(f"Plants: {[str(plant) for plant in bed.plants]}\n")
# Method: harvest
print(c, "Harvest <bed>\n")
bed_yield = bed.harvest()
print(f"yield: {bed_yield}\n")
print(bed, end="\n\n")
# %%
# Same as above without prints
plants = [FruitBearingPlant("strawberry", 3, Fruit("strawberry", 33)),
FruitBearingPlant("tomato", 4, Fruit("tomato", 16)),
RootVegetable("carrot", 1.2, 77, 2)]
bed = Bed("in the garden")
amount = 20
bed.water(amount)
for plant in plants:
bed.plant_plant(plant)
periods = amount // len(plants)
for _ in range(periods):
bed.update()
bed.update()
bed_yield = bed.harvest()
# %%
bed_yield
# %%