我正在尝试创建一个朋友词典,我可以在其中添加一个朋友并将他的信息放在那里。
我想用朋友的名字作为钥匙,两个号码,两封电子邮件和他住在哪里的信息。
我的问题是我的程序在询问数字和电子邮件时崩溃,我不知道我做错了什么。
我使用了 append 函数,因为每个朋友的号码都保存在一个列表中。我不想要一个我想修复我的新程序,所以我可以理解为什么它会失败。
我想做的另一件事是不打印我最后创建的空字典,它是一个包含字典的列表(每个朋友都是一个字典),所以我想我应该说从位置 1 到最后,但我想有更好的方法,在这里我发布我的代码,错误是当我要求第一个和第二个电话和邮件时。
def add_contact(friends):
contact = {}
contact["name"]=input("name: ")
for i in range(2):
contact["phone"][i]=input("phone: ") #Here it crashes
for i in range(2):
contact["mail"][i]=input("mail: ") #Here too
contact["street"]=input("street: ")
contact["housenum"]=input("housenum: ")
contact["cp"]=input("cp: ")
contact["city"]=input("city: ")
friends.append(contact)
friends = [{"name":[{"telf":[0]*2},{"mail":[0]*2},
{"street":"","housenum":"","cp":"", "city":""}]}] #This is the list im creating to fill it with friends information, the first dictionary in the list is an empty dictionary which i dont want to print.
add_contact(friends)
print(friends)
慕的地10843
GCT1015
相关分类