我希望用 Python 创建一个电话簿系统,如下图所示,它允许用户添加、删除、更新和查找电话号码。
请参阅下面我的代码。选项 1、2、3 和 5 工作正常,但是选项 4 不起作用,我不知道为什么。当我使用选项 1 添加新条目,然后立即尝试使用选项 4 查找该条目时,它告诉我该条目不存在,但它显然不存在。我是否遗漏了什么?(相关代码部分以“elif Selection == 4:”开头)
def intro():
welcome = (" \n"
"#####################################\n"
"MYPY PHONE BOOK\n"
"#####################################\n"
"1 : Add New Entry\n"
"2 : Delete Entry\n"
"3 : Update Entry\n"
"4 : Lookup Number\n"
"5 : QUIT\n"
"")
selection = input(welcome)
return int(selection)
phoneBook = {}
while True:
selection = intro()
if selection == 1:
number = input("Enter phone number: ")
name = input("Enter name: ")
if number in phoneBook:
print(" \n"
"* Number already in phonebook *")
else:
phoneBook.update({number : name})
print(" \n"
"* Name and number added to phone book *")
print(phoneBook)
elif selection == 2:
number2 = input("Enter phone number to be deleted: ")
if number2 in phoneBook:
phoneBook.pop(number2)
print(" \n"
"* Number deleted from phone book *")
print(phoneBook)
UYOU
相关分类