Foreword
I have read the python tutorial before. I really read it myself. The python program can understand it, but it is difficult to implement. It is more difficult to implement some code by myself, find the reason for the job, and read the book and type the code seriously. Later, I saw this topic and wanted to review the commonly used data types in python that I learned before. It took a while, and the programming was done.
Python realizes business card management system
Can realize the following functions:
Business Card Management System
- Add business card
- Delete business card
- Modify business card
- Query business cards
- Exit system
- Show all business cards
Add business card
The programming idea first creates a temporary templist variable, adds [name] (https://www.zalou.cn/tag/xingming), [phone] (https://www.zalou.cn/tag/shouji) number, address and other information through the templist.append() method, and then appends the templist list to the mainList list.
def increMem(aList):
tempList =[]
tempName =input("Enter the name of the new business card:")
tempList.append(tempName)while True:
tempPhone =input("Enter the phone number of the new contact:")if tempPhone.isnumeric():breakelse:print("Input error, re-enter")
tempList.append(tempPhone)
tempAddr =input("Enter the new contact address:")
tempList.append(tempAddr)print("Enter the new contact information:")showList(tempList)
aList.append(tempList)
note:
Mobile numbers are all numbers, you can use the list.isnumeric() method to determine whether they are pure numbers string, not return False
Delete business card
Programming idea: first calculate whether it is empty, if it is empty, return, then first locate the index value of the deleted contact, and finally delete the contact through del()function.
def delMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempName =input("Enter the contact you want to delete:")for mumList in aList:if tempName != mumList[0]:
i +=1continueelse:showList(aList[i])while True:
tempIn =input("Delete this contact: Y(Yes)t N(no) :")if tempIn =="Y" or tempIn =="y":del(aList[i])print("successfully deleted!")return
elif tempIn =="N" or tempIn =="n":print("Re-enter the contact!")delMem(aList)returnelse:print("Input error, re-enter!")if i ==len(aList):print("The entered contact hot does not exist, please enter again!")delMem(aList)
note:
What if the deleted contact does not exist? To traverse the mainList, each element is an element of a list structure. If the contact to be deleted is not equal to numLinst[0], continue, i increments by 1. If traversing all of them, there is none, then i = len(aList), then judge that the contact does not exist, and enter again.
Modify business card
Modify the business card, first locate and then modify.
def modMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempList =input("Enter the contact to be modified:")for numList in aList:if tempList != numList[0]:
i +=1continueelse:
tempInf =input("Enter the modified information:")if tempInf.isnumeric():
numList[1]= tempInf
else:
numList[2]= tempInf
if i ==len(aList):print("The input is wrong, try again!")modMem(aList)
note:
The is.numeric() method, judging, if it is all numbers, the modified phone number, otherwise it is the address.
Find a business card
Position first, then output. Pay attention to the analysis when there is no contact
def LocaMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempList =input("Enter the contact you need to find:")for numList in aList:if tempList != numList[0]:
i +=1continueelse:showList(numList)if i ==len(aList):print("The input is wrong, try again!")modMem(aList)
**Complete **Programblock
def men():print("t*****************")print("t Business card management system n")print("t 1.Add business card n")print("t 2.Delete business card n")print("t 3.Modify business card n")print("t 4.Query business card n")print("t 5.Exit the system n")print("t 0.Show all business cards n")print("t*****************")
def increMem(aList):
tempList =[]
tempName =input("Enter the name of the new business card:")
tempList.append(tempName)while True:
tempPhone =input("Enter the phone number of the new contact:")if tempPhone.isnumeric():breakelse:print("Input error, re-enter")
tempList.append(tempPhone)
tempAddr =input("Enter the new contact address:")
tempList.append(tempAddr)print("Enter the new contact information:")showList(tempList)
aList.append(tempList)
def showList(aList):print("first name: %s"%aList[0],"phone:%s"%aList[1],"address:%s"%aList[2],"n")
def showMem(aList):iflen(aList)==0:print("No contact!")for mumList in aList:print("first name: %s"%mumList[0],"phone:%s"%mumList[1],"address:%s"%mumList[2],"n")
def delMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempName =input("Enter the contact you want to delete:")for mumList in aList:if tempName != mumList[0]:
i +=1continueelse:showList(aList[i])while True:
tempIn =input("Delete this contact: Y(Yes)t N(no) :")if tempIn =="Y" or tempIn =="y":del(aList[i])print("successfully deleted!")return
elif tempIn =="N" or tempIn =="n":print("Re-enter the contact!")delMem(aList)returnelse:print("Input error, re-enter!")if i ==len(aList):print("The entered contact hot does not exist, please enter again!")delMem(aList)
def modMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempList =input("Enter the contact to be modified:")for numList in aList:if tempList != numList[0]:
i +=1continueelse:
tempInf =input("Enter the modified information:")if tempInf.isnumeric():
numList[1]= tempInf
else:
numList[2]= tempInf
if i ==len(aList):print("The input is wrong, try again!")modMem(aList)
def LocaMem(aList):
i =0iflen(aList)==0:print("No contact, please add a contact first!")return
tempList =input("Enter the contact you need to find:")for numList in aList:if tempList != numList[0]:
i +=1continueelse:showList(numList)if i ==len(aList):print("The input is wrong, try again!")modMem(aList)if __name__ =="__main__":
mainList =[]men()while True:
index =input("Enter task number:")if not index.isnumeric():print("Please enter the index number (1-4):")continue
index =int(index)
# Traverse business cards
if index ==0:showMem(mainList)
# Add business card
if index ==1:increMem(mainList)if index ==2:delMem(mainList)if index ==3:modMem(mainList)if index ==4:LocaMem(mainList)if index ==5:print("Exit system!")break
The above is the whole content of this article, I hope it will be helpful to your study, and I hope you can support website (zalou.cn).
Recommended Posts