Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # All Objects and Classes - Lab and Exericse - Python Fundamentals 100/100.
- #Objects and Classes - Lab
- ////////////////////////////////////////////////////////////////////////////
- #01. Comment
- class Comment:
- def __init__(self, username, content, likes=0):
- self.username = username
- self.content = content
- self.likes = likes
- ////////////////////////////////////////////////////////////////////////////
- # 02. Party
- class Party:
- def __init__(self):
- self.people = []
- total_people = Party()
- command_line = input()
- while not command_line == 'End':
- current_name = command_line
- total_people.people.append(current_name)
- command_line = input()
- print(f'Going: {", ".join(total_people.people)}')
- print(f"Total: {len(total_people.people)}")
- ////////////////////////////////////////////////////////////////////////////
- # 03. Email
- class Email:
- def __init__(self, sender, receiver, content):
- self.sender = sender
- self.receiver = receiver
- self.content = content
- self.is_sent = False
- def get_info(self):
- return f"{self.sender} says to {self.receiver}: " \
- f"{self.content}. Sent: {self.is_sent}"
- def send(self):
- self.is_sent = True
- message = input()
- emails_data = []
- while not message == 'Stop':
- details = message.split(' ')
- sender = details[0]
- receiver = details[1]
- content = details[2]
- all_info = Email(sender, receiver, content)
- emails_data.append(all_info)
- message = input()
- sent_emails = list(map(int, input().split(', ')))
- for x in sent_emails:
- emails_data[x].send()
- for email in emails_data:
- print(email.get_info())
- ////////////////////////////////////////////////////////////////////////////
- # 04. Zoo
- class Zoo:
- __animals = 0
- def __init__(self, name):
- self.name = name
- self.mammals = []
- self.fishes = []
- self.birds = []
- def add_animal(self, species, name):
- if species == 'mammal':
- self.mammals.append(name)
- elif species == 'fish':
- self.fishes.append(name)
- elif species == 'bird':
- self.birds.append(name)
- Zoo.__animals += 1
- def get_info(self, species):
- result = ""
- if species == 'mammal':
- result += f'Mammals in {self.name}: {", ".join(self.mammals)}\n'
- elif species == 'fish':
- result += f'Fishes in {self.name}: {", ".join(self.fishes)}\n'
- elif species == 'bird':
- result += f'Birds in {self.name}: {", ".join(self.birds)}\n'
- result += f"Total animals: {Zoo.__animals}"
- return result
- zoo_name = input()
- my_zoo = Zoo(zoo_name)
- total_animals = int(input())
- for _ in range(1, total_animals + 1):
- animal_info = input().split(' ')
- current_species = animal_info[0]
- name = animal_info[1]
- my_zoo.add_animal(current_species, name)
- species_to_print = input()
- print(my_zoo.get_info(species_to_print))
- ////////////////////////////////////////////////////////////////////////////
- # 05. Circle
- class Circle:
- __pi = 3.14
- def __init__(self, diameter):
- self.diameter = diameter
- self.radius = diameter / 2
- def calculate_circumference(self):
- return 2 * (Circle.__pi * self.radius)
- def calculate_area(self):
- return Circle.__pi * (self.radius ** 2)
- def calculate_area_of_sector(self, angle):
- return (angle / 360) * Circle.__pi * (self.radius ** 2)
- ////////////////////////////////////////////////////////////////////////////
- # Objects and Classes - Exericse
- ***************************************************************************************************************
- #01. Storage
- class Storage:
- storage = []
- def __init__(self, capacity: int):
- self.capacity = capacity
- def add_product(self, product: str):
- if len(self.storage) < self.capacity:
- Storage.storage.append(product)
- def get_products(self):
- return Storage.storage
- ***************************************************************************************************************
- #02. Weapon
- class Weapon:
- def __init__(self, bullets: int):
- self.bullets = bullets
- def shoot(self):
- if self.bullets > 0:
- self.bullets -= 1
- return 'shooting...'
- return 'no bullets left'
- def __repr__(self):
- return f'Remaining bullets: {self.bullets}'
- ***************************************************************************************************************
- # 03. Catalogue
- class Catalogue:
- def __init__(self, name: str):
- self.name = name
- self.products = []
- def add_product(self, product_name: str):
- self.products.append(product_name)
- def get_by_letter(self, first_letter: str):
- by_letter = []
- for letter in self.products:
- if letter[0] == first_letter:
- by_letter.append(letter)
- return by_letter
- def __repr__(self):
- self.products = sorted(self.products)
- final_products = '\n'.join(self.products)
- return f"Items in the {self.name} catalogue:\n{final_products}"
- ***************************************************************************************************************
- # 04. Town
- class Town:
- def __init__(self, name: str, latitude="0°N", longitude="0°E"):
- self.name = name
- self.latitude = latitude
- self.longitude = longitude
- def set_latitude(self, latitude):
- result1 = self.latitude = latitude
- return result1
- def set_longitude(self, longitude):
- result2 = self.longitude = longitude
- return result2
- def __repr__(self):
- return f"Town: {self.name} | Latitude: {self.latitude} | Longitude: {self.longitude}"
- ***************************************************************************************************************
- # 05. Class
- class Class:
- __student_count = 22
- def __init__(self, name: str):
- self.name = name
- self.students = []
- self.grades = []
- def add_student(self, name: str, grade: float):
- Class.__student_count -= 1
- if Class.__student_count > 0:
- self.students.append(name)
- self.grades.append(grade)
- def get_average_grade(self):
- result = sum(self.grades) / len(self.students)
- return float(f'{result:.2f}')
- def __repr__(self):
- result = f"The students in {self.name}: {', '.join(self.students)}. Average grade: {self.get_average_grade()}"
- return result
- return result
- ***************************************************************************************************************
- # 06. Inventory
- class Inventory:
- def __init__(self, __capacity: int):
- self.items = []
- self.__capacity = __capacity
- def add_item(self, item: str):
- if self.__capacity > 0:
- self.items.append(item)
- self.__capacity -= 1
- return "not enough room in the inventory"
- def get_capacity(self):
- result = len(self.items)
- return result
- def __repr__(self):
- to_print = f"{', '.join(self.items)}"
- return f"Items: {to_print}.\nCapacity left: {self.__capacity}"
- ***************************************************************************************************************
- # 07. Articles
- class Article:
- def __init__(self, title: str, content: str, author: str):
- self.title = title
- self.content = content
- self.author = author
- def edit(self, new_content: str):
- self.content = new_content
- return self.content
- def change_author(self, new_author: str):
- self.author = new_author
- return self.author
- def rename(self, new_title: str):
- self.title = new_title
- return self.title
- def __repr__(self):
- result = f"{self.title} - {self.content}: {self.author}"
- return result
- ***************************************************************************************************************
- # 08. Vehicle
- class Vehicle:
- def __init__(self, type, model, price):
- self.type = type
- self.model = model
- self.price = price
- self.owner = None
- def buy(self, money: int, owner: str):
- if (money >= self.price) and (self.owner is None):
- price_dif = float(f"{(money - self.price):.2f}")
- result = f"Successfully bought a {self.type}. Change: {price_dif:.2f}"
- self.owner = owner
- return result
- elif money < self.price:
- return 'Sorry, not enough money'
- elif self.owner is not None:
- return 'Car already sold'
- def sell(self):
- if self.owner is not None:
- self.owner = None
- else:
- return 'Vehicle has no owner'
- def __repr__(self):
- if self.owner is not None:
- return f"{self.model} {self.type} is owned by: {self.owner}"
- return f"{self.model} {self.type} is on sale: {self.price}"
- ***************************************************************************************************************
- # 09. Movie
- class Movie:
- __watched_movies = []
- def __init__(self, name, director, watched=False):
- self.name = name
- self.director = director
- self.watched = watched
- def change_name(self, new_name: str):
- self.name = new_name
- return self.name
- def change_director(self, new_director: str):
- self.director = new_director
- return self.director
- def watch(self):
- if self.name not in Movie.__watched_movies:
- self.watched = True
- Movie.__watched_movies.append(self.name)
- def __repr__(self):
- to_print = len(Movie.__watched_movies)
- return f"Movie name: {self.name}; Movie director: {self.director}. Total watched movies: {to_print}"
- ***************************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement