import os
contact = {}
def display_contact():
print("Name\t\tContact Number")
for key in contact:
print("{}\t\t{}".format(key,contact.get(key)))
while True:
choice = int(input(" 1.Add new contact \n 2. Search contact \n 3. Display contact\n 4. Edit contact \n 5. Delete contact \n 6. Sort List \n 7. Print \n 8. Exit \n Enter "))
if choice == 1:
name = input("Enter the contact name ")
phone = input("Enter number")
contact[name] = phone
elif choice == 2:
search_name = input("Enter contact name ")
if search_name in contact:
print(search_name, "'s contact number is ", contact[search_name])
else:
print("Name is not found in contact book")
elif choice == 3:
if not contact:
print("Empty Phonebook")
else:
display_contact()
elif choice == 4:
edit_contact = input("Enter the contact to be edited ")
if edit_contact in contact:
phone = input("Enter numer")
contact[edit_contact]=phone
print("Contact Updated")
display_contact()
else:
print("Name is not found in contact book")
elif choice == 5:
del_contact = input("Enter the contact to be deleted ")
if del_contact in contact:
confirm = input("Do you want to delete this contact Yes or No? ")
if confirm == 'Yes' or confirm == 'yes':
contact.pop(del_contact)
display_contact
else:
print("Name is not found in phone book")
else:
break
`
我有這個,但不知道這是否是正確的代碼使用或放置在哪里。我需要幫助按字母對聯系人串列進行排序并添加列印代碼,以便像列印機一樣列印。有什么幫助嗎?謝謝
strs = ['aa', 'BB', 'zz', 'CC']
print(sorted(strs)) ## ['BB', 'CC', 'aa', 'zz'] (case sensitive)
print(sorted(strs, reverse=True)) ## ['zz', 'aa', 'CC', 'BB']
我不擅長編碼,任何幫助都很好,分類和列印到列印件會很棒。謝謝你的幫助。
uj5u.com熱心網友回復:
你可以這樣寫 display_contact() 。
def display_contact():
for name, number in sorted((k,v) for k, v in contact.items()):
print(f'Name={name}, number={number}')
您可能希望根據所需的排序順序將關鍵引數傳遞給sorted() 。
請注意,函式名稱用詞不當,因為它會列印所有聯系人
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531690.html
