#1
class GameCharacter:
def __init__(self, name, damage, health):
self.name = name
self.damage = damage
self.health = health
def attack(self):
print(f"{self.name} наносит {self.damage} урона")
def take_damage(self, amount):
self.health -= amount
self.health = 0 if self.health < 0 else self.health
print(f"{self.name} получил {amount} урона."
f" Осталось здоровья: {self.health}")
Char1 = GameCharacter('Machachev', 100, 1000)
Char1.attack()
Char1.take_damage(52)
Char1.take_damage(67)
Char1.take_damage(69)
print(Char1.health)
#2
class Cart:
def __init__(self, owner):
self.owner = owner
self.items = []
def add_item(self, name, price):
self.items.append({"name": name, "price": price})
def get_total(self):
sum_prices = 0
for elements in self.items:
sum_prices += elements['price']
return sum_prices
def show_items(self):
for els in self.items:
print(f"{els['name']} - {els['price']}")
if len(self.items) == 0:
print("Корзина пуста")