class Critter(object): """Виртуальный питомец""" def __init__(self.name): print("Появилась на свет новая зверюшка") self.name = name def __str__ (self): rep = "Объект класса Critter\n" rep += "Имя:" + self.name + "\n" return rep def talk(self): print("Привет. Меня зовут" , self.name "\n") # Basic part crit1 = Critter(Бобик) crit1.talk() crit2 = Critter(Мурзик) crit2.talk() print("Вывод объекта crit1 на экран") print(crit1) print("Непосредственный доступ к атрибуту crit1.name") print(crit1.name) input("\nЖмякни Enter") |
1 answer
Line
print("Привет. Меня зовут" , self.name "\n") It lacks the + sign, it should be like this:
print("Привет. Меня зовут", self.name + "\n") Row
def __init__(self.name): replaced by:
def __init__(self, name): And the names of animals must be enclosed in quotes:
crit1 = Critter("Бобик") crit2 = Critter("Мурзик") More about class objects: https://docs.python.org/3/tutorial/classes.html#class-objects
- Then it's better to use formatting:
print("Привет. Меня зовут {}\n".format(self.name)). For Python 3.6:print(f"Привет. Меня зовут {self.name}\n")- gil9red
|