In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python to make a small library management system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python to make a small library management system.
target
Understand the analysis methods of the internal functions of the class in the process of object-oriented development.
Understand common system functions
System requirement
The idea of object-oriented programming is used to complete the development of the library management system. The details are as follows:
System requirements: book information is stored in files
System functions, add, delete, modify, save, query, display all book information, exit the system and other functions.
Program file analysis and design
Role Analysis: books, Management system
Note:
One role, one program file.
The main entry of the project, which can be defined as main.py
Main function
Based on object-oriented: realize books:
Add featur
Delete function
Modify function
Query function
Show all
Save to a file
Load data and other functions.
Program file
Program entry: main.py
Book file: book.py
Manage system files: systemmgr.py
1.1.4 Program Code
Book file code
Book file: book.py
Demand: books include: title, type, SN number, etc.
Add _ _ str__ magic method to make it easy to view book object information
Code:
Class Book (object): def _ init__ (self,name,type,sn): self.name=name self.type=type self.sn=sn def _ str__ (self): return f'{self.name}, {self.type}, {self.sn}'
Manage file program code
Demand:
The system functions are recycled to perform different functions according to the function serial number entered by the user.
Steps:
Load data
Show function menu
Perform different operation functions according to user input
Define system functions: add, delete, save, etc.
Define the class of the management system: used to initialize the code that stores the management system in the class.
Define a list of list_page empty books for easy storage.
Def _ init__ (self):
Self.list_page= []
Add a static method to display the function menu
@ staticmethod def show_menu (): print ("Please select the following features") print ("1, add new books") print ("2, delete new books") print ("3, modify new books") print ("4, query related books") print ("5, show all books") print ("6, save book information") print ("7") Load book information ") print (" 9, exit the system ")
The program entry function, which starts the function run () that the program executes
Def run (self): while True: self.show_menu () int_num=int (input ("Please enter the function serial number") if int_num==1:# add self.add_book () if int_num== 2while True # Delete self.del_book () elif int_num==3:# modify self.update_book () Elif int_num==4:# query self.search_book () elif int_num==5:# display self.show_book () elif int_num==6:# Save self.save_book () elif int_num== 7 Vol # load self.load_book () elif int_num== 9: print ("exit the system") break else: print ("input error!")
Program entry file program code
Function: import the system management module and start the library management system.
Main.py
From xm19code.systemgr import Systemmgrif _ _ name__=='__main__': rr = Systemmgr () rr.run () program code listing:
The specific code is shown below:
Class Book (object): def _ init__ (self,name,type,sn): self.name=name self.type=type self.sn=sn def _ str__ (self): return f'{self.name}, {self.type} {self.sn} 'from xm19code.systemgr import Systemmgrif _ _ name__=='__main__': rr = Systemmgr () rr.run () # from book import * from xm19code.book import * class Systemmgr (object): def _ init__ (self): self.list_page= [] # Loop @ staticmethod def show_menu (): print ("Please select the following features") print ("1") Add new book ") print (" 2, delete new book ") print (" 3, modify new book ") print (" 4, query related books ") print (" 5, show all books ") print (" 6, save book information ") print (" 7, add book information ") print (" 9 ") Exit the system ") def run (self): while True: self.show_menu () int_num=int (input (" Please enter the function serial number ") if int_num==1:# add self.add_book () if int_num== 2while True # Delete self.del_book () Elif int_num==3:# modify self.update_book () elif int_num==4:# query self.search_book () elif int_num==5:# display self.show_book () elif int_num==6:# save self.save_book () elif Int_num = = 7 elif int_num # load self.load_book () elif int_num = = 9: print ("exit the system") break else: print ("input error!") Def add_book (self): # add name=input ("Please enter the name of the book") type=input ("Please enter the classification of the book") sn = input ("Please enter the sn of the book") book=Book (name,type) according to the fields entered by the user Sn) self.list_page.append (book) print (self.list_page) print (book) def update_book (self): names = input ("Please enter the book to be modified:") for i in self.list_page: if i.name==names: i.name=input ("Please enter the name of the book to be modified:") I.type = input ("Please enter the type of book to be modified:") i.sn = input ("Please enter the book to be modified SN:") print (f' modified the book information successfully: book {i.name} Type {i.type}, sn is: {i.sn}') break else: print ("No book!") Def del_book (self): names=input ("Please enter the book to delete:") # for Loop query for i in self.list_page: if i.name = = names: self.list_page.remove (I) print ("deleted successfully!") Break else: print ("no book") def show_book (self): print ('book name\ t\ t type\ t\ t\ tSN') for i in self.list_page: print (f' {i.name}\ t\ t\ t {i.type}\ t\ t {i.sn}') def save_book (self) : # Open the file f = open ('book.data' 'w') # File writer converts list [Book object] into dictionary new_list = [i.book object] print (new_list) f.write (str (new_list)) # File closes f.close () Def search_book (self): names=input ("Please enter the name of the book to be queried:") # for Loop query for i in self.list_page: if i.name==names: print (book information queried by f': book {i.name},'f' type {i.type} Sn is: {i.sn}') break else: print ("no book") def load_book (self): try: f=open ('book.data','r') except: f=open (' book.data') 'w') else: the data read by the data=f.read () # file are all strings and the data inside the string is dictionary data # so it is necessary to convert the data type in the conversion dictionary and store it in the book list new_list=eval (data) self.list_page= [book (I ['name'], I [' type']] I ['sn']) for i in new_list] print (' book\ t\ t type\ t\ tSN') for i in self.list_page: print (f'{i.name}\ t\ t {i.type}\ t {i.sn}') finally: # close the file f.close () program generation Code running result: C:\ Python\ Python38\ python.exe D:/pythonProject/xm19code/main.py Please select the following function 1 Add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9, exit system please enter function serial number 7 book type SNpy py 1234 Please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9 To exit the system, please enter the function serial number 2, please enter the book to be deleted: 22 if there is no this book, please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9, exit the system, enter function serial number 3, please enter the book to be modified: 22 there is no such book! Please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9, exit the system, enter the function serial number 1, enter the name of the book 1, please enter the classification 1 of the book, enter the sn1 [,] 1 of the book, please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5 Display all books 6, save book information 7, load book information 9, exit the system, please enter function serial number 6 [{'name':' py', 'type':' py', 'sn':' 1234'}, {'name':' 1', 'type':' 1', 'sn':' 1'}] Please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5 Display all books 6, save book information 7, load book information 9, exit the system, please enter function serial number 1, please enter the name of the book 2, please enter the category 2 of the book, please enter the sn2 [,] 2 Magi 2 of the book, please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9 Please enter the function serial number 4 to exit the system, please enter the name of the book to be queried: 1 Book information queried: book 1, type 1 Magi sn: 1 Please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9. Please enter the function serial number 4 to exit the system, please enter the name of the book to be queried: 1 Book information queried: book 1, type 1 Magi sn: 1 Please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9. To exit the system, enter the function serial number 6 [{'name':' py', 'type':' py', 'sn':' 1234'}, {'name':' 1, 'type':' 1, 'sn':' 1'}, {'name':' 2, 'type':' 2, 'sn':' 2'}] Please select the following function 1, add new book 2, and delete new book 3 Modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9, exit the system, please enter function serial number 7, book type SNpy py 12341 1 12 22, please select the following function 1, add new book 2, delete new book 3, modify new book 4, query related book 5, display all books 6, save book information 7, load book information 9 Please enter the function serial number to exit the system. I believe you have a deeper understanding of "how to use Python to make a small library management system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.