我在Python中创建一个简单的地址簿程序。每当我通过命令行菜单输入多个联系人,然后按" b"列出所有联系人,它只显示我输入的最后一个联系人。如何让程序保存所有联系人?
# Import Collections to use Ordered Dictionary
import collections
# The main class
def main():
# Print the menu for program
print """
Contact Book App
a) New Contact
b) List Contacts
c) Search Contacts
d) Delete Contact
e) Quit
"""
# Creates an empty ordered dictionary
contact = collections.OrderedDict()
# Sets the loop as 1
loop = 1
# Create a while loop for the menu that keeps looping for user input until loop = 0
while loop == 1:
# Asks for users input from 1-5
userInput = raw_input("Please select an option: ").lower()
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
# Confirmation prompt
print "Contact Added!"
#For Debugging Purposes
# print(contact)
# OPTION 2 : LIST ALL CONTACTS
elif userInput == "b":
print "Listing Contacts"
print(contact)
# OPTION 3 : SEARCH CONTACTS
elif userInput == "c":
print "Searching Contacts"
print "Please Enter Contact Name"
# OPTION 4 : DELETE A CONTACT
elif userInput == "d":
print "Deleting Contact"
# OPTION 5 : QUIT PROGRAM
elif userInput == "e":
print "Quitting Contact Book"
loop = 0
else:
print "I did not understand your input"
main()
这是我的输出:
Contact Book App
a) New Contact
b) List Contacts
c) Search Contacts
d) Delete Contact
e) Quit
Please select an option: a
Enter name: Dave Smith
Enter phone: 5553451212
Enter email: dsmith@gmail.com
Contact Added!
Please select an option: a
Enter name: John Doe
Enter phone: 4445433232
Enter email: jdoe@hotmail.com
Contact Added!
Please select an option: b
Listing Contacts
OrderedDict([('name', 'John Doe'), ('phone', '4445433232'), ('email', 'jdoe@hotmail.com')])
Please select an option:
正如您所看到的,只显示最后一个条目John Doe
并且Dave Smith
已被覆盖。
答案 0 :(得分:2)
制作一个列表并添加新的联系人:
contacts = []
...
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact = collections.OrderedDict()
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
contacts.append(contact)
...
答案 1 :(得分:1)
contacts = []
# Sets the loop as 1
loop = 1
# Create a while loop for the menu that keeps looping for user input until loop = 0
while loop == 1:
# Asks for users input from 1-5
userInput = raw_input("Please select an option: ").lower()
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact = collections.OrderedDict()
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
contacts.append(contact)
# Confirmation prompt
print "Contact Added!"
#For Debugging Purposes
# print(contact)
# OPTION 2 : LIST ALL CONTACTS
elif userInput == "b":
print "Listing Contacts"
print(contacts)
[...]
答案 2 :(得分:1)
每次向用户询问新的联系人详细信息时,都会覆盖以前创建的单一联系人。相反,您必须维护联系人列表,创建新联系人并将其添加到列表中。另外,我建议创建class Contact
而不是仅使用字典。
class Contact:
def __init__(self, name, phone, mail):
self.name = name
self.phone = phone
self.mail = mail
def __repr__(self):
return "Contact(%r, %r, %r)" % (self.name, self.phone, self.mail)
此外,您可以使用break
退出循环而不是使用变量。在main
(摘录)中:
...
contacts= [] # initialize list of contacts
while True:
userInput = raw_input("Please select an option: ").lower()
if userInput == "a":
name = raw_input("Enter name: ")
phone = raw_input("Enter phone: ")
email = raw_input("Enter email: ")
contacts.append(Contact(name, phone, email)) # create and add new contact
print "Contact Added!"
elif userInput == "b":
print "Listing Contacts"
print(contacts) # use contacts list here
... more options
elif userInput == "e":
print "Quitting Contact Book"
break # use break here
else:
print "I did not understand your input"
答案 3 :(得分:0)
每次使用键名称'在词典中创建新条目时你覆盖了前一个。最好创建一个新的词典列表,并将每个人存储为该列表中的单独词典。