1. 程式人生 > >python實現名片管理系統

python實現名片管理系統

——————————————————————————————————————————————————————

想要掌握什麼知識,就需要在什麼花費時間,浮躁的看,過目即忘,再加上學完了,並不使用,過幾天都忘關了。

學以致用!學習Python時候,也要想想,如果是C/C++如何程式設計實現。一遍一遍的寫,一遍一遍的學習。

明天會感謝自己今天的努力!

——————————————————————————————————————————————————————

前言

之前看過一遍的python教程,真的是自己看過一遍,python的程式能看懂,但是很難去實現。比較困難的自己實現一些程式碼,找工作原因,自己又認認真真的看書,敲程式碼,後來看到了這個題目,想把之前學習的python常用的資料型別複習下。花了一點兒時間,程式設計實現了。

python實現明信片管理系統

能實現如下功能: *****************
名片管理系統

1.新增名片

2.刪除名片

3.修改名片

4.查詢名片

5.退出系統

0.顯示所有名片

*****************

新增名片

程式設計思路 先建立一個臨時的 templist 變數,通過 templist.append()方法,增加,姓名,手機號,地址等資訊,然後把templist列表追加到 mainList列表中。
def increMem(aList):
    tempList = [] 
    tempName = input("輸入新建名片名字:")
    tempList.append(tempName)
    while True:
        tempPhone = input("輸入新建聯絡人手機號:") 
        if tempPhone.isnumeric(): break
        else: print("輸入有誤,重新輸入")    
    tempList.append(tempPhone)
    tempAddr = input("輸入新建聯絡人地址:")
    tempList.append(tempAddr)
    print("輸入新建聯絡人資訊:")
    showList(tempList)
    aList.append(tempList)
注意:  手機號都是數字,可以通過 list.isnumeric()方法判斷是否是純數字字串,不是返回False

刪除名片

程式設計思想:首先盤算是否是空,如果是空返回,然後先定位刪除聯絡人的索引值,最後通過del()函式刪除聯絡人。
def delMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempName = input("輸入要刪除的聯絡人:")
    for mumList in aList:
        if tempName != mumList[0] :
            i += 1
            continue
        else:
            showList(aList[i])
            while True:
                tempIn = input("是否刪除此聯絡人: Y(是)\t N(否) :")
                if tempIn =="Y" or tempIn == "y":
                    del(aList[i])
                    print("刪除成功!")
                    return 
                elif tempIn == "N" or tempIn == "n":
                    print("重新輸入聯絡人!")
                    delMem(aList)
                    return
                else:
                    print("輸入有誤,重新輸入!")                    
    if i == len(aList):
        print("輸入的聯絡熱不存在,請重新輸入!")
        delMem(aList)
注意: 如果刪除的聯絡人不存在,怎麼處理?對mainList遍歷,每一個元素都是一個 list 結構的元素。如果 要刪除的聯絡人不等於numLinst[0],則繼續,i 自增1.如果遍歷所有的,都沒有,則i = len(aList),則判斷聯絡人不存在,重新輸入。

修改名片

修改名片,先定位後修改。
def modMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempList = input("輸入需要修改的聯絡人:")
    for numList in aList:
        if tempList != numList[0] :
            i += 1
            continue
        else:
            tempInf = input("輸入修改的資訊:")
            if tempInf.isnumeric():
                numList[1] = tempInf
            else:
                numList[2] = tempInf
    if i == len(aList):
        print("輸入有誤,重新輸入!")
        modMem(aList)
注意: is.numeric()方法,判斷,全是數字,則是修改的是電話號碼,否則則是地址。

查詢名片

先定位,再輸出。注意分析沒有聯絡人時候情況
def LocaMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempList = input("輸入需要查詢的聯絡人:")
    for numList in aList:
        if tempList != numList[0] :
            i += 1
            continue
        else:
            showList(numList)
    if i == len(aList):
        print("輸入有誤,重新輸入!")
        modMem(aList)             

完整的程式塊

def men():
    print("\t*****************")
    print("\t  名片管理系統\n")
    print("\t  1.新增名片\n")
    print("\t  2.刪除名片\n")
    print("\t  3.修改名片\n")
    print("\t  4.查詢名片\n")
    print("\t  5.退出系統\n")
    print("\t 0.顯示所有名片\n")
    print("\t*****************")
def increMem(aList):
    tempList = [] 
    tempName = input("輸入新建名片名字:")
    tempList.append(tempName)
    while True:
        tempPhone = input("輸入新建聯絡人手機號:") 
        if tempPhone.isnumeric(): break
        else: print("輸入有誤,重新輸入")    
    tempList.append(tempPhone)
    tempAddr = input("輸入新建聯絡人地址:")
    tempList.append(tempAddr)
    print("輸入新建聯絡人資訊:")
    showList(tempList)
    aList.append(tempList)
def showList(aList):
       print("名字: %s"%aList[0],\
         "電話:%s"%aList[1], \
         "地址:%s"%aList[2],"\n")
def showMem(aList):
    if len(aList) == 0:
        print("沒有聯絡人!")
    for mumList in aList:
        print("名字: %s"%mumList[0],\
             "電話:%s"%mumList[1], \
             "地址:%s"%mumList[2],"\n")
def delMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempName = input("輸入要刪除的聯絡人:")
    for mumList in aList:
        if tempName != mumList[0] :
            i += 1
            continue
        else:
            showList(aList[i])
            while True:
                tempIn = input("是否刪除此聯絡人: Y(是)\t N(否) :")
                if tempIn =="Y" or tempIn == "y":
                    del(aList[i])
                    print("刪除成功!")
                    return 
                elif tempIn == "N" or tempIn == "n":
                    print("重新輸入聯絡人!")
                    delMem(aList)
                    return
                else:
                    print("輸入有誤,重新輸入!")                    
    if i == len(aList):
        print("輸入的聯絡熱不存在,請重新輸入!")
        delMem(aList)
def modMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempList = input("輸入需要修改的聯絡人:")
    for numList in aList:
        if tempList != numList[0] :
            i += 1
            continue
        else:
            tempInf = input("輸入修改的資訊:")
            if tempInf.isnumeric():
                numList[1] = tempInf
            else:
                numList[2] = tempInf
    if i == len(aList):
        print("輸入有誤,重新輸入!")
        modMem(aList)
def LocaMem(aList):
    i = 0
    if len(aList) == 0 : 
        print("沒有聯絡人,請先新增聯絡人!")
        return
    tempList = input("輸入需要查詢的聯絡人:")
    for numList in aList:
        if tempList != numList[0] :
            i += 1
            continue
        else:
            showList(numList)
    if i == len(aList):
        print("輸入有誤,重新輸入!")
        modMem(aList)             
            
if __name__ == "__main__":            
    mainList = []
    men()
    while True:
        index = input("輸入任務編號:")
        if not index.isnumeric(): 
            print("請輸入索引編號(1-4):")
            continue
        index = int(index)
        #遍歷名片
        if index == 0:
            showMem(mainList)
        #增加名片
        if index == 1: 
            increMem(mainList)
        if index == 2:
            delMem(mainList)
        if index == 3:
            modMem(mainList)
        if index == 4:
            LocaMem(mainList)
        if index == 5:
            print("退出系統!")
            break