-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoo.py
92 lines (76 loc) · 3.09 KB
/
zoo.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
from animal import Animal
import random
class Zoo:
def __init__(self, animals, capacity, budget):
self.animals = animals
self.capacity = capacity
self.budget = budget
#self.available_meat = 300
#self.available_grass = 200
def names_for_species(self, new_animal):
names_list = []
for animal in self.animals:
if animal.species == new_animal.species:
names_list.append(animal.name)
return names_list
def accommodate(self, new_animal):
names_list = []
names_list = self.names_for_species(new_animal)
if len(self.animals) == self.capacity:
return "No more space!"
elif new_animal.name not in names_list:
self.animals.append(new_animal)
return True
elif new_animal.name in names_list:
return "There is such species with the same name. Rename your animal"
def get_income(self):
animal_brings = 60
self.budget += animal_brings * len(self.animals)
def outcome(self):
total_meat = 0
total_grass = 0
grass_price = 2
meat_price = 4
for animal in self.animals:
total_grass += animal.grass_eaten
total_meat += animal.meat_eaten
self.budget -= total_grass * grass_price + total_meat * meat_price
def clear_dead_animals(self, life_expectancy):
for animal in self.animals:
############## life_expectancy #############
if animal.die() is True:
self.animals.remove(animal)
def gender_baby(self):
random_number = random.random()
if random_number > 0.5:
return "male"
return "female"
def name_baby(self, baby):
name = input("You have a newborn {}. Give it a name: ".format(baby.species))
return name
def does_name_exist(self, name, animal):
names_list = self.names_for_species(animal)
if name in names_list:
return True
else:
return False
def reproduce(self, animal1, animal2):
min_age = 24
cannot_get_pregnant = 6
if animal1.age < min_age or animal2.age < min_age:
return "Cannot reproduce. Animals are too young"
# Must discuss these changes!
if animal1.species == animal2.species and animal1.gender != animal2.gender:
if animal1.gender == "female" and animal1.age - animal1.last_pregnancy >= cannot_get_pregnant:
animal1.last_pregnancy = animal1.age + animal1.gestation_period
elif animal2.gender == "female" and animal2.age - animal2.last_pregnancy >= cannot_get_pregnant:
animal2.last_pregnancy = animal2.age + animal2.gestation_period
baby = Animal(animal1.species, 0, None, self.gender_baby(), animal1.newborn_weight)
while True:
baby.name = self.name_baby(baby)
if self.does_name_exist(baby.name, baby) is False:
break
self.accommodate(baby)
return baby
else:
return "Cannot reproduce"