Skip to content

Instantly share code, notes, and snippets.

@gdsotirov
Last active November 22, 2024 13:28
Show Gist options
  • Select an option

  • Save gdsotirov/fa4de3f714453110776ff099fd97b43b to your computer and use it in GitHub Desktop.

Select an option

Save gdsotirov/fa4de3f714453110776ff099fd97b43b to your computer and use it in GitHub Desktop.
# 1.Сьздай клас Animal, който представлява общата информация за животно:
# • Полета: name, species (вИД), age, health (стойност от 1 до 10, която показва здравословното сьстояние
#на животното).
# • Метод print_info(), който отпечатва информация за животното вьв формат „Име: ..., Вид: ..., Възраст: ...,
#Здраве: ...".
# 2.Сьздай клас Mamal, който наследява Animal:
# • Допълнителни полета: fur_color (цвят на козината) и diet (тип хранене: тревопасно, месоядно или всеядно).
# • Метод print_info(), който извежда сьщо и цвета на козината и типа хранене.
# 3.Сьздай клас Bird, който наследява Animal:
# • Допълнителни полета: wing_span (размах на крилете в сантиметри) и can_fly (булева стойност, която
#указва дали птицата може да лети).
# • Метод print_info(), който извежда сьщо и размаха на крилете и дали може да лети.
# 4.Сьздай клас Reptile, който наследява Animal: |
# • Допълнителни полета: 15_venomous (булева стойност, указваща дали е отровно) И preferred_temperature
#(предпочитана температура в градуси).
# • Метод print_info(), който извежда сьщо и информация за отровност и предпочитана температура.
# 5.Сьздай клас Zoo, който управлява списк от клетки и животни:
# • Полета: enclosures - речник, кодето ключовете са номера на клетки (като int), а стойностите са списьци
#с животни в тях.
# • Метод add_animal(animal, enclosure_number), който добавя животно в дадена клетка. Ако клетката не
#сьществува, създава нова клетка и добавя животното.
# • Метод resove_animal, който премахва животно от клетка по име.
# • Метод list_animals_in_enclosure, който отпечатва информация за всички животни в дадена клетка.
# • Метод transfer_animal, който прехврля животно от една клетка в друга.
# • Метод find_animals_by_species(, КОЙТО извежда информация за всички животни от даден вид в зоопарка.
def b2str(val):
if val:
return "Да"
else:
return "Не"
class Animal:
def __init__(self, name, species, age, health):
self.name = name
self.species = species
self.age = age
self.health = health
def print_info(self):
print('Име: ' + str(self.name) + ' Вид: ' + str(self.species) + ' Възраст: ' + str(self.age) + ' Здраве: ' + str(self.health))
class Mammal(Animal):
def __init__(self, name, species, age, health, fur_color, diet):
super().__init__(name, species, age, health)
self.fur_color = fur_color
self.diet = diet
def print_info(self):
super().print_info()
print('Цвят на козината: ' + str(self.fur_color) + ' Тип хранене: ' + str(self.diet))
class Bird(Animal):
def __init__(self, name, species, age, health, wing_span, can_fly):
super().__init__(name, species, age, health)
self.wing_span = wing_span
self.can_fly = can_fly
def print_info(self):
super().print_info()
print('Разнах на криле: ' + str(self.wing_span) + ' Може ли да лети: ' + b2str(self.can_fly))
class Reptile(Animal):
def __init__(self, name, species, age, health, venomous, preferred_temperature):
super().__init__(name, species, age, health)
self.venomous = venomous
self.preferred_temperature = preferred_temperature
def print_info(self):
super().print_info()
print('Отровно ли е: ' + str(self.venomous) + ' Предпочитана температура: ' + str(self.preferred_temperature))
class Zoo:
enclosures = {} # start with an empty zoo
def add_animal(self, animal, enclosure_number):
if str(enclosure_number) not in self.enclosures:
self.enclosures[str(enclosure_number)] = []
self.enclosures[str(enclosure_number)].append(animal)
def remove_animal(self, enclosure_number, name):
idx = 0
for animal in self.enclosures[str(enclosure_number)]:
idx = idx + 1
if animal.name == name:
return self.enclosures[str(enclosure_number)].pop()
def list_animals_in_enclosure(self, enclosure_number):
print(f"Животни в клетка № {enclosure_number}:")
for animal in self.enclosures[str(enclosure_number)]:
animal.print_info()
def transfer_animal(self, enc_from, enc_to, name):
animal = self.remove_animal(enc_from, name)
if animal:
self.add_animal(animal, enc_to)
def find_animals_by_species(self, kind):
for num in self.enclosures.keys():
print(f"Животни от тип {kind} в клетка № {num}: ")
for animal in self.enclosures[num]:
if animal.species == kind:
animal.print_info()
bear = Mammal("Bear", "bears", 3, 9, "black", "honey,fruits")
fox = Mammal("Fox", "carnivores", 5, 7, "red", "eggs")
wolf = Mammal("Wolf", "carnivores", 2, 10, "grey", "meet")
rabbit = Mammal("Rabbit", "herbivores", 1, 8, "white", "carrots")
zoo = Zoo()
zoo.add_animal(bear, 1)
zoo.add_animal(fox, 2)
zoo.add_animal(wolf, 2)
zoo.add_animal(rabbit, 3)
zoo.list_animals_in_enclosure(2)
print("\n")
wolf = zoo.remove_animal(2, "Wolf")
zoo.list_animals_in_enclosure(2)
print("\n")
zoo.add_animal(wolf, 2)
zoo.transfer_animal(2, 4, "Wolf")
zoo.find_animals_by_species("carnivores")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment