[英]How to use Dictionary in While Loop?
我正在用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
被覆盖。
列出并添加新的联系人:
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)
...
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)
[...]
每次您要求用户提供新的联系人详细信息时,都会覆盖以前创建的单个联系人。 相反,您必须维护一个联系人列表,创建一个新联系人并将该联系人添加到列表中。 另外,我建议创建一个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"
每次您在字典中使用键“名称”创建新条目时,都会覆盖前一个。 最好创建一个新的词典列表,并将每个人存储为该列表中的单独词典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.