我的代码如下所示....
但是当我输入垃圾邮件和号码等数据时,以前的数据会被删除。
所以我想在一个键中创建多个值......(我认为使用列表是一种好方法)
例如,
键:垃圾邮件 - 值:01012341111、01099991234、
键:垃圾邮件 - 值:01032938962、01023421232、01023124242
总之,我想得到这个打印(附图)
这是我的代码:
enter code here
phonebook = dict()
ntype = ['spam', 'friend', 'family', 'etc']
trash =[]
spam = []
friend = []
family = []
etc = []
while True:
a = input("Input your number and ntype : ")
b = a.split()
i = 0
j = 0
if a == 'exit':
print("end program")
break
exit()
elif a == "print spam":
print("*** spam numbers: ")
print('*', phonebook['spam'])
elif a == "print numbers":
print("*** numbers:")
for t in ntype:
try:
print("**", t)
print('*',phonebook[t])
except KeyError:
continue
print("** trash")
print("*", phonebook['trash'])
else:
while True:
try:
if ntype[j] in b:
for k in b:
if list(k)[0] == '0' and len(k) >= 10 and len(k) <= 11:
if k in phonebook.values():
print("Already Registered")
else:
phonebook[ntype[j]] = k
print(phonebook)
break
else:
j+=1
except IndexError:
if list(b[i])[0] == '0' and len(b[i]) >= 10 and len(b[i]) <= 11:
if b[i] in phonebook.values():
print("Already Registered")
else:
phonebook['trash'] = b[i]
print(phonebook)
break
else:
break