gpt4 book ai didi

python - 为什么我没有从第二次创建对象中获得值? - Python

转载 作者:行者123 更新时间:2023-12-01 08:50:43 27 4
gpt4 key购买 nike

我正在遵循我在网上找到的这个练习:

http://gsl.mit.edu/media/programs/sri-lanka-summer-2012/materials/t_hw1.pdf

这将使我更加了解我想要完成的任务。

当我在 lastname 上调用 search_contact <-- (Simpson) 函数时,我确实得到了第一个对象:

Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com 

但我也想获取第二个对象,因此输出如下所示:

Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com
Marge, Simpson, -- Phone Number: 5559352365, --Email: marge@mail.com

仍在练习掌握面向对象的编程范例。如果有不清楚的地方请告诉我。感谢您的帮助,伙计们。

这是我到目前为止所拥有的:

class Person:
def __init__(self, firstname, lastname, phone_number, email):
self.firstname= firstname
self.lastname= lastname
self.phone_number = phone_number
self.email = email

def __str__(self):
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(self.firstname, self.lastname, self.phone_number, self.email)
template = template.replace('[','').replace(']','').replace("'", '')
return template.format(self)

class AddressBook(Person):
def __init__(self):
self.book = {}

def add_contact(self, p):
self.book[p] = p
return self.book

def search_contact(self, lastname):
for p in self.book:
if p.lastname == lastname:
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.firstname, p.lastname, p.phone_number, p.email)
return template

if __name__ == '__main__':
# Bob = Person('Bob', 'Lop', '5559358150', 'bob@mail.com)
# print(Bob)
# Joe = Person('Joe', 'Roe', '5551940325',['joe@mail.com', 'roe@mail.com'])
# print(Joe)

a = AddressBook()
added = a.add_contact(Person('Homer', 'Simpson', '5559355899', 'homer@mail.com'))
# print(added)
added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365', 'marge@mail.com'))
# print(added_1)
search = a.search_contact('Simpson')
print(search)

最佳答案

您必须修复 Person,__init__() 中的错误 - 参数顺序不正确(或者提供创建 Person(...)< 时翻转的名称)。

这个

   def search_contact(self, lastname):
for p in self.book:
if p.lastname == lastname:
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.lastname, p.firstname, p.phone_number, p.email)
return template

返回

如果您从函数返回,那么您就完成了。您可以yield(使其成为生成器)或收集列表中的所有匹配项并返回它。无论哪种方式,您都需要调整您的打印代码,因为现在您会返回一个列表/生成器。

<小时/>

列表示例(生成器更复杂):

class Person:
def __init__(self, firstname, lastname, phone_number, email): # FIX
self.lastname = lastname
self.firstname = firstname
self.phone_number = phone_number
self.email = email

def __str__(self):
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(
self.lastname, self.firstname, self.phone_number, self.email)
template = template.replace('[','').replace(']','').replace("'", '')
return template.format(self)

class AddressBook:
def __init__(self):
self.book = {}

def add_contact(self, p):
self.book[p] = p
return self.book

def search_contact(self, lastname):
hits = []
for p in self.book:
if p.lastname == lastname:
hits.append(
'{}, {}, -- Phone Number: {}, --Email: {}'.format(
p.lastname, p.firstname, p.phone_number, p.email))
return hits

if __name__ == '__main__':
a = AddressBook()
added = a.add_contact(Person('Homer', 'Simpson', '5559355899',
'homer@mail.com'))
# print(added)
added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365',
'marge@mail.com'))
# print(added_1)
search = a.search_contact('Simpson')
for s in search:
print(s)

输出:

Simpson, Homer, -- Phone Number: 5559355899, --Email: homer@mail.com
Simpson, Marge, -- Phone Number: 5559352365, --Email: marge@mail.com

关于python - 为什么我没有从第二次创建对象中获得值? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53143953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com