Python Crash Course #8 - Classes

preview_player
Показать описание
In this Python crash course tutorial series, you'll learn all the basics of Python from the ground up.

🚀🥷🏼Get access to the Python Crash Course on Net Ninja Pro:

🔥🥷🏼 Access Premium Courses with a Net Ninja Pro subscription:

🔗🥷🏼 Check out Clear Code for more of his own tutorials:

Рекомендации по теме
Комментарии
Автор

My solution (before watching the instructor's solution):

class Entity:
def __init__(self, damage):
self.damage = damage

def attack(self):
print(f"attack {self.damage}")

class Monster(Entity):
def __init__(self, health, damage):
super().__init__(damage)
self.health = health
self.damage = damage

def __str__(self):
return f"a monster with {self.health} hp"

monster = Monster(40, 10)
monster.attack()
print(monster)

briantoon
Автор

Why u store the class in a variable and the call the variable for the print when u can directly call the class and the variable inside of the class with print? If its only the storage as a purpose then ok

nibi
Автор

Thumbnail is incorrect - Tools instead of Classes

lokeshsenthilkumar
Автор

class Entity:
def __init__(self, damage_amount):
self.damage_amount = damage_amount

def attack(self):
print('Attack:', self.damage_amount)

class Monster(Entity):
def __init__(self, damage_amount, health):

self.health = health

def attack(self):
super().attack()
remaining_health = self.health - self.damage_amount
print(f'a monster with {remaining_health} hp')
return remaining_health


entity = Entity(100)
monster = Monster(99, 100)
monster.attack()

trevor-marloy