1. 程式人生 > >用python字典寫的一個簡單的通訊錄小系統

用python字典寫的一個簡單的通訊錄小系統

直接上程式碼:

mydic={}
while True:
    print("="*7,"通訊錄管理系統","="*7)
    print("1.增加姓名和手機")
    print("2.刪除姓名")
    print("3.修改手機號")
    print("4.查詢所有使用者")
    print("5.根據姓名查詢手機號")
    print("6.退出")
    print("="*29)
    a=input("請選擇要執行的專案:")
    if a=="1":
        name=input("請輸入聯絡人姓名:")
        tel=input("請輸入手機號:")
        if tel.isdigit() and len(tel)==11:
            mydic[name]=tel
            print("當前手機內聯絡人:",mydic)
        else:
            print("輸入有誤")
    elif a=="2":
        name=input("請輸入要刪除的姓名:")
        tel=mydic.pop(name)
        print("已刪除:"+name+tel)
        print("當前手機聯絡人:",mydic)
    elif a=="3":
        name=input("請輸入要修改的手機號姓名:")
        tel=input("請輸入新的手機號:")
        mydic[name]=tel
        print("修改成功!")
        print("當前手機內聯絡人:",mydic)
    elif a=="4":
        print("當前手機內聯絡人:",mydic)
    elif a=="5":
        name=input("請輸入要查詢的聯絡人姓名:")
        print(name,mydic[name])
        print("")
    elif a=="6":
        print("感謝使用通訊錄系統")
        break
    else:
        print("輸入有誤")