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 write the code of the library management system realized by python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write the code of the python library management system". In the daily operation, I believe that many people have doubts about how to write the code of the python library management system. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to write the code of the python library management system". Next, please follow the editor to study!

Software python

Write a database to store data in code in advance, that is, a .txt file

This is one of the data I simulated.

D1 name_list.txt'[{"user name": "admin", "password": "123"," name ":" Zhang San "}, {" user name ":" aaa "," password ":" 123", "name": "Li Si"}] 'with open (r "name_list.txt", "w") as f: f.write (D1) d2dream' [{"number": 1001, "title": "A Dream of Red Mansions" "author": "Cao Xueqin", "loan status": "available"},\ {"number": 1002, "Book title": "java tutorial", "author": "one Day", "loan status": "available",\ {"number": 1003, "title": Bible, "author": "Jesus", "Lending status": "loaned"} \ {"Ref": 1004, "title": "Collection of Li Bai's Poems", "author": "Li Bai", "Lending status": "available"}\] 'with open (r "books.txt", "w") as f: f.write (D2)

The data is ready to begin:

One step is to read the data in the database and convert it to a string, because the next data is string processing

Def readUser (): with open (r "name_list.txt", "r") as f: jsondata=f.read () userlist=json.loads (jsondata) return userlist

This step is to write external data to the database

Def writeData (listb): jsondata=json.dumps (listb,ensure_ascii=False) with open (r "name_list.txt", "w") as f: f.write (jsondata) print ("- data written successfully!")

1. User login:

Def login (): userlist=readUser () uname=input ("Please enter your user name:") upwd=input ("Please enter your password:") msg= "failed" for user in userlist: if uname==user ["user name"] and upwd==user ["password"]: print ("Congratulations", user ["name"] "login success") msg= "success" if msg== "failure": print ("login failed" Please log in again! ") Return msg

two。 Display a list of books:

Def Showbooklist (): Booklist=readBook () print ("* * Book Catalog * *") for data in Booklist: print (data ["number"], "\ t", data ["title"], "\ t\ t\ t", data ["author"] "\ t\ t\ t", data ["loan status"]) print ("- -")

3. Add a book

Def addBook (): Booklist=readBook () numlist= [] for book in Booklist: numlist.append (book ["number"]) newnum=max (numlist) + 1 Bname=input ("Please enter the title of the book:") writer=input ("Please enter the author:") condi= "can borrow" newBooklist= {"number": newnum, "title": Bname, "author": writer, "loan status": condi} Booklist.append (newBooklist) writeBook (Booklist)

4. Delete a book

Def delbook (): Booklist=readBook () Bnum=int (input ("Please enter the book number to be deleted:") Bname=input ("Please enter the name of the book to be deleted:") for book in Booklist: if Bnum==book ["number"] or Bname==book ["title"]: Booklist.remove (book) print ("Book has", book ["title"], "deleted") writeBook (Booklist)

5. Borrow books

Def lendbook (): Booklist=readBook () Bnum=int (input ("Please enter the book number to borrow:") Bname=input ("Please enter the name of the book to borrow:") for book in Booklist: if Bnum==book ["number"] or Bname==book ["title"]: if book ["Lending status"] = "borrowable": print ("this book is on the shelf" ) a=int (input ("borrow greetings 1, please press 0:")) if aquires 1: book ["loan status"] = "lent" else: break else: print ("this book has been lent out" Borrow again after returning ") writeBook (Booklist)

6. Return the book

Def returnbook (): Booklist=readBook () Bnum=int (input ("Please enter the book number to be returned:") Bname=input ("Please enter the name of the book to be returned:") for book in Booklist: if Bnum==book ["number"] or Bname==book ["title"]: if book ["Lending status"] = "lent": book ["Lending status"] = "OK" Borrow "print (book [title"] ("returned") else: print (book ["title of the book"], "the book is on the shelf, no need to return it") writeBook (Booklist)

Finally, the general program of all modules is called:

While 1 success 1: print ("* Library Management system 2.0 managers *") msg=login () if msg== "success": print ("1. Show all books;\ n2. Books are on the shelves. Books off the shelves;\ n4. Borrow;\ n5. Return;\ n6. Exit ") print ("-") while 2customers 2: choice=int (input (" Please enter the business number (1-6): ") if choice==1: Showbooklist () elif choice==2: addBook () elif choice==3: delbook () elif choice==4: lendbook () elif choice==5: returnbook () elif choice==6: break else: print ("there is no such service Please re-select ") continue

This is the effect picture after the implementation.

At this point, the study on "how to write the code of the python library management system" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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