Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement address Book Management system with python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "python how to achieve address book management system", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python how to achieve address book management system" article.

one。 Project requirements

The use of functions to achieve address book management system, the contents of the address book contains names, contact information, WeChat, e-mail and other information that need to be saved. Include at least two kinds of name and contact information. The functions of the management system include the functions of adding, deleting, modifying, querying, displaying and saving information.

II. Project steps

1. Use custom functions to implement each function

(1) function menu: displays the functions that the project should have

(2) add: enter the information to be added

(3) Delete: enter the name of the contact to be deleted and delete all its information

(4) modify: enter the name of the contact to be modified and modify its information

(5) query: enter the name of the contact to be queried, traverse the list and get its information

(6) display: display all contact information

(7) Save: save data to a file

(8) main function: first display the menu, prompt the user to enter the operation code, and call the corresponding function

two。 When you add, delete, or modify information, save it to a file synchronously

Use the list as a global variable, and call the save function when the list information changes

3. Write a module, call the module in the main function, and test its function. Complete one, test one, ensure that all modules run correctly, and the information is stored in a text file.

4. You can use lists, tuples, and dictionaries to store address book information. If you only store your name and mobile phone number now, select a dictionary and convert it to a tuple and save it in the list.

5. Initially, you create a text file with the name of the student and enter an empty list [].

Third, the project code requirements:

1. The code should have the necessary comments to explain its function.

two。 Have test results for each module.

3. If there is a screenshot, the screenshot should be clear.

Code

1. Code description

The topic requires functions to be called from the module, so two python files need to be created in the same folder: manage.py and main.py. Among them, the function is designed in manage.py, and the function in manage.py is called in main.py.

2.manage.py

Attention: 1. The required names are all reasonable input by default, and the input content is not judged and filtered. The default open file already exists, and the file status is not judged, but based on whether the student exists in the list information. There is no operation using a dictionary 4. If you want to save all the student information in the same txt file, simply comment out the file_name renamed part of each function. The way to modify the information is to delete the original content and rewrite the new content. The reason for this is that the computer is running out of power. This can be used as your own test board to redesign and write 6. I don't know what the purpose of the last step of the experiment is, so I didn't do it. Because the list is used to judge the student content, rerun the program list after the file is saved successfully, that is, the information can no longer be queried. Print all the information can be taken under the folder to read multiple files, traverse all the txt files under the directory, read out and print, please write your own 2021-6-16 new note: 1. When you modify the content of a file, if you change the name together, the file name will be different from the name in the file content. 2. When you modify the contents of the file, if you change the name to another name that has already appeared, there will be a duplicate name in the list. There is no operation dealing with duplicate names at first. In the case of the same name, if the information of the name is modified, the information of the location where the name first appears will be modified. So, if error 1 and error 2 occur at the same time, the file will, um. Strung out The dictionary storage information Student = [] path = "D:\\" # the file save path is empty by default, that is, it is recommended to create a new folder under the current folder and modify the path file_name = path + "student.txt" # File default location # display command def print_message (): print ("*" * ") print (" * * 1. Add student information ") print (" * * 2. " Delete student information ") print (" * * 3. " Modify student information ") print (" * * 4. Find student information ") print (" * * 5. " View all information ") print (" * * 6. Check the operation menu ") # add information def add (): print (" Please enter the name and mobile phone number you need to add Separated by a space: ") # list A = input (). Split (") temp_str = "" for i in A: temp_str + = I + "" if temp_str! ='/ nasty: # prevent mistyping Student.append (A) # join the list file_name = path+A [0] + ".txt" print (temp_str File=open (file_name,'w')) # create a file And write to the file if there is a file with the same name The original content will be overwritten # dictionary # A = input (). Split (") # Name.append (A [0]) # d [A [0]] = A [1] # print (" added successfully ") # save (A [0]) # Update student information # deletion information def delete (): print (" Please enter to delete Name of: ") name = input () # Dictionary deletion # del d [name] # Name.remove (name) # Delete student information exist = 0 for i in range (len (Student)): # exists the student if study [I] .count (name) > 0: if name = = Student [I] [0]: file _ name = path + name + ".txt" # delete the information in the list Student.pop (I) # delete the information in the file lines = [l for lin open (file_name) "r") if l.find (name, 0, len (name))! = 0] # temporary deletion method fd = open (file_name, "w") # for example: to delete 'Zhang San' But 'Zhang three four' will also be deleted fd.writelines (lines) fd.close () print ("deleted successfully") exist = 1 # tag to determine whether the student exists or not: break else: exist = 0 if exist = 0: print ("No information about the student was found!") Def delete_2 (name For i in range (len (Student)): # if study [I] .count (name) > 0: if name = = Student [I] [0]: del Student [I] break # from the file Delete student information lines = [l for lin open (frankn) "r") if l.find (name, 0, len (name))! = 0] fd = open (frankn, "w") fd.writelines (lines) fd.close () # query information def search (): print ("Please enter the name to be queried:") name = input () # Dictionary query # print ("query result:", name D [name]) exist = 0 for i in range (len (Student)): # it is a bit superfluous for the student to judge this step, but to simplify printing the results that cannot be found, he or she did the if study [I] .count (name) > 0: if name = = Student [I] [0]: print ("query result:") For temp in Student [I]: print (temp End= "") print () exist = 1 # tag to determine whether a student exists: break else: exist = 0 if exist = = 0: print ("Information not found for this student!") # change file information def change_file (): print ("enter the name of the student who needs to modify information:") name = input () exist = 0 for i in range (len (Student)): # exists the student if study [I] .count (name) > 0: if name = = Student [I] [0]: # found the student # file_name = path + name + ".txt" print ("enter all the information of the student after modification:") A = input () .split (") temp_str ="for i in A: Temp_str + = I + "" if temp_str! ='/ nasty: # prevent mistyping Student.append (A) # add to the list delete_2 (name File_name) # delete this information first print (temp_str, file=open (file_name,'a')) # create a file And rewrite the file print ("modified successfully") exist = 1 # tag to judge whether the student exists or not break else: exist = 0 if exist = 0: print ("No information about the student was found!") # display information def printf (): print ("all the information is as follows:") for i in Student: for j in i: print (jPowerend = ") print ()

3.main.py

Import manageprint ("* *") print ("Welcome to Student Information Management system") manage.print_message () # print command instruction print ("* *" ) while (True): print ("Please enter the action you need to perform (enter'q' to exit the program):") choice = input () if choice = = '1percent: manage.add () # add if choice = =' 2percent: manage.delete () # delete if choice = = '3percent: manage.change_file () # change if choice = =' 4percent: manage. Search () # check if choice = = '5percent: manage.printf () # print if choice = =' 6percent: manage.print_message () # print command instruction if choice = = 'qcow: print ("exit!") Break above is about the content of this article on "how to implement the address book management system in python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report