【发布时间】:2018-11-12 13:34:42
【问题描述】:
我在使用 delContact 方法时遇到了困难,因为我想删除类似于 delVar 的对象,但我不知道如何尝试 all = None 或 del all。但是当我在 delContact() 之后调用 show 方法/函数时,我想删除的值仍然显示
class Phonebook:
def __init__(self,name,type,email,phone):
self.name = name
self.type = type
self.email = email
self.phone = phone
print('Adding {} as a Contact with an email of {} and a phone number of {}'.format(self.name,self.email,self.phone))
def tell(self):
print('Name: {} | Type: {} | Email: {} | Phone: {}'.format(self.name,self.type,self.email,self.phone))
def retName(self):
return self.name
All = []
def addContact():
name = input('Enter your the name please: ')
type = input('Enter the type of the Contact Please: ')
email = input('Enter the the email of the Contact Please: ')
phone = input('Enter the Phone number of the Contact Please: ')
merge = Phonebook(name,type,email,phone)
All.append(merge)
def show():
for all in All:
all.tell()
def delContact():
show()
delVar = input('Enter the name you want to delete: ')
for all in All:
if all.retName() == delVar:
print(all.retName())
all = ''
else:
continue
addContact()
addContact()
show()
delContact()
show()
【问题讨论】:
-
您忘记将
self添加为方法的默认参数。还要创建一个对象,然后调用方法。如果您不打算将您的方法用于实例,那么您将无缘无故地拥有一个构造函数 -
@PrakashPalnati 你到底在说什么?
标签: python python-3.x