Answer the question
In order to leave comments, you need to log in
Why does it show the contents of the cell instead of the dictionary data?
Guys, tell me why when printing out the data of the dictionary, instead of the data of the class, the interpreter gives out the data of the memory cell?
class Owners:
def __init__(self, name, phone_number, age):
try:
self.name = name if type(name) == 'str' else str(name)
self.phone_number = phone_number if type(phone_number) == 'str' else str(phone_number)
self.age = age if type(age) == 'int' else int(age)
except Exception:
print('Error Type')
def __str__(self):
return ''.join(self.name + ' ' + self.phone_number + ' ' + str(self.age))
def new_owner():
name = input('Имя: ' )
telephone = input('Телефон: ')
age = input('Возраст: ')
owner = Owners(name, telephone, age)
return owner
class Vehicle:
def __init__(self, num, color, vehicle_type, health):
self.num = num if type(num) == 'str' else str(num)
self.color = color if type(color) == 'str' else str(color)
self.vehicle_type = vehicle_type if type(vehicle_type) == 'str' else str(vehicle_type)
self.health = health if type(health) == 'int' else int(health)
def __str__(self):
return ''.join(self.num + ' ' + self.color + ' ' + self.vehicle_type + ' ' + str(self.health))
def heal(self):
self.health = 100
def condition(self, damage):
self.health -= damage
def new_vehicle(owner):
num = input('Номeр автомобиля: ' )
color = input('Цвет автомобиля: ' )
vehicle_type = input('Тип автомобиля: ')
health = input('Состояние автомобиля (%): ')
vehicle = Vehicle(num, color, vehicle_type, health)
return vehicle
data_autowner = {}
def main():
auto_owner = new_owner()
auto_vehicle = new_vehicle(auto_owner)
data_autowner[auto_owner.name] = auto_owner, auto_vehicle
print (data_autowner)
main() </source>
Answer the question
In order to leave comments, you need to log in
By the way:
self.name = name if type(name) == 'str' else str(name)
self.name = name if isinstance(name, str) else str(name)
return ''.join(self.name + ' ' + self.phone_number + ' ' + str(self.age))
return ' '.join((self.name, phone_number, str(self.age)))
return self.name + ' ' + self.phone_number + ' ' + str(self.age)
return f'{self.name} {self.phone_number} {self.age}'
return '{0} {1} {2}'.format(self.name, self.phone_number, self.age)
def __repr__(self):
return ' '.join((self.name, self.phone_number, str(self.age)))
1. It's strange that your join is done. It takes an iterable object, and concatenates everything through your string.
Why they folded the lines and put them there is not clear
2. It should return an object, because you are printing several classes at once, also in a tuple. Print just auto_owner or auto_vehicle
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question