使用list和dict在python中创建多个电话目录记录
我正在使用python创建电话目录程序,首先我是python新手,在这里我创建了类并声明了全局列表和dict,因为我想在调用testcreate函数时存储,我们输入将存储在列表中的名称和电话,该列表将通过保存函数存储在dict中。要显示电话簿数据,只需调用getdata函数,但它不起作用,请提前感谢使用list和dict在python中创建多个电话目录记录,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我正在使用python创建电话目录程序,首先我是python新手,在这里我创建了类并声明了全局列表和dict,因为我想在调用testcreate函数时存储,我们输入将存储在列表中的名称和电话,该列表将通过保存函数存储在dict中。要显示电话簿数据,只需调用getdata函数,但它不起作用,请提前感谢 class testdl(): testlist=[] testdic={} def testcreate(): name = input("en
class testdl():
testlist=[]
testdic={}
def testcreate():
name = input("enter name : ")
phone = input("enter phone : ")
testdl.save(name,phone)
def save(n,p):
testdl.testlist[n]=p
testdl.testdic = testdl.testlist
def getdata():
print(testdl.testdic)
>>> tdll = testdl()
>>> tdll.testcreate()
**Error Message :**
Traceback (most recent call last):
File "<pyshell#81>", line 1, in <module>
tdll.testcreate()
TypeError: testcreate() takes 0 positional arguments but 1 was given
>>> testdl.testcreate()
enter name : srikanth
enter phone : 1234567890
**Error Message :**
Traceback (most recent call last):
File "<pyshell#82>", line 1, in <module>
testdl.testcreate()
File "<pyshell#79>", line 7, in testcreate
testdl.save(name,phone)
File "<pyshell#79>", line 9, in save
testdl.testlist[n]=p
TypeError: list indices must be integers or slices, not str
class testdl():
testlist=[]
testdic={}
def testcreate():
名称=输入(“输入名称:”)
电话=输入(“输入电话:”)
testdl.save(姓名、电话)
def保存(n,p):
testdl.testlist[n]=p
testdl.testdic=testdl.testlist
def getdata():
打印(testdl.testdic)
>>>tdll=testdl()
>>>tdll.testcreate()
**错误消息:**
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
tdll.testcreate()
TypeError:testcreate()接受0个位置参数,但提供了1个
>>>testdl.testcreate()
输入名称:srikanth
输入电话:1234567890
**错误消息:**
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
testdl.testcreate()
testcreate中第7行的文件“”
testdl.save(姓名、电话)
文件“”,第9行,保存
testdl.testlist[n]=p
TypeError:列表索引必须是整数或片,而不是str
错误1:
类中的每个方法都需要self作为第一个参数,它引用当前实例
def testcreate(self):
name = input("enter name : ")
phone = input("enter phone : ")
self.save(name,phone)
错误2:
def save(self, n,p):
self.testlist[n]=p
列表仅接受整数作为索引
您将收到的错误 错误3:
self.testlist
为空=>索引器错误:找不到索引
错误4:
列表不是字典
(在save()中)
可能的解决办法
class testdl():
testdic={}
def testcreate(self, ):
name = input("enter name : ")
phone = input("enter phone : ")
self.save(name,phone)
def save(self, n,p):
self.testlist[n]=p
def getdata(self, n):
print(self.testdic[n])
错误1:
类中的每个方法都需要self作为第一个参数,它引用当前实例
def testcreate(self):
name = input("enter name : ")
phone = input("enter phone : ")
self.save(name,phone)
错误2:
def save(self, n,p):
self.testlist[n]=p
列表仅接受整数作为索引
您将收到的错误 错误3:
self.testlist
为空=>索引器错误:找不到索引
错误4:
列表不是字典
(在save()中)
可能的解决办法
class testdl():
testdic={}
def testcreate(self, ):
name = input("enter name : ")
phone = input("enter phone : ")
self.save(name,phone)
def save(self, n,p):
self.testlist[n]=p
def getdata(self, n):
print(self.testdic[n])
显然,您的函数可以是staticmethod,无需创建类。如果确实要创建类:
class Testdl:
def __init__(self): # initialize the value
self.testlist=[]
self.testdic={}
self.name = None
self.phone = None
def testcreate(self):
self.name = input("enter name : ")
self.phone = input("enter phone : ")
self.save()
def save(self):
self.testdic[self.name] = self.phone # maybe you need a dict to save them not a list
self.testlist.append(self.testdic) # then you append it in the list
def getdata(self):
print(self.testdic)
testdl = Testdl() # create a instance
testdl.testcreate() # call the function
testdl.getdata() # print the data
如果要记录三组数据,请使用:
testdl = Testdl()
for i in range(3):
testdl.testcreate()
testdl.getdata()
显然,您的函数可以是staticmethod,无需创建类。如果确实要创建类:
class Testdl:
def __init__(self): # initialize the value
self.testlist=[]
self.testdic={}
self.name = None
self.phone = None
def testcreate(self):
self.name = input("enter name : ")
self.phone = input("enter phone : ")
self.save()
def save(self):
self.testdic[self.name] = self.phone # maybe you need a dict to save them not a list
self.testlist.append(self.testdic) # then you append it in the list
def getdata(self):
print(self.testdic)
testdl = Testdl() # create a instance
testdl.testcreate() # call the function
testdl.getdata() # print the data
如果要记录三组数据,请使用:
testdl = Testdl()
for i in range(3):
testdl.testcreate()
testdl.getdata()
就像上面说的,需要一个参数(我们总是使用
self
)。你应该使用dict来保存它,而不是一个列表。@jizhaosama:你能简单地解释一下吗,因为我是python新手,我正在为这个解决方案搜索2天。就像上面说的,需要一个参数(我们总是使用self
)。你应该使用dict来保存它,而不是列表。@jizhaosama:你能简单地解释一下吗,因为我是python新手,我正在为这个解决方案搜索2天。