python模擬實現圖書管理系統
本文實例為大傢分享瞭python實現圖書管理系統的具體代碼,供大傢參考,具體內容如下
軟件python
事先用代碼寫一個存儲數據的數據庫,也就是,.txt文件
這是我模擬的一個數據
d1='[{"用戶名": "admin", "密碼": "123", "姓名": "張三"},{"用戶名": "aaa", "密碼": "123", "姓名": "李四"}]' with open(r"name_list.txt","w") as f: f.write(d1) d2='[{"編號":1001, "書名": "紅樓夢", "作者": "曹雪芹", "借出狀態": "可借"},\ {"編號":1002, "書名": "java教程","作者": "齊一天", "借出狀態": "可借"},\ {"編號":1003, "書名": "聖經","作者": "耶穌", "借出狀態": "已借出"},\ {"編號":1004, "書名": "李白詩集","作者": "李白", "借出狀態": "可借"}\ ]' with open(r"books.txt","w") as f: f.write(d2)
數據準備好開始:
一步是讀取數據庫中的數據,將其轉換為字符串,因為接下來的數據都是字符串的處理
def readUser(): with open(r"name_list.txt","r") as f: jsondata=f.read() userlist=json.loads(jsondata) return userlist
這一步是將外部數據寫入數據庫
def writeData(listb): jsondata=json.dumps(listb,ensure_ascii=False) with open(r"name_list.txt","w") as f: f.write(jsondata) print("----------------數據寫入成功!")
1.用戶登錄:
def login(): userlist=readUser() uname=input("請輸入您的用戶名:") upwd=input("請輸入您的密碼:") msg="失敗" for user in userlist: if uname==user["用戶名"] and upwd==user["密碼"]: print("恭喜",user["姓名"],"登錄成功") msg="成功" if msg=="失敗": print("登錄失敗,請從新登錄!") return msg
2.顯示圖書列表:
def Showbooklist(): Booklist=readBook() print("**************************圖書目錄**************************") for data in Booklist: print(data["編號"],"\t",data["書名"],"\t\t\t",data["作者"],"\t\t\t",data["借出狀態"]) print("---------------------------------------------------")
3.添加圖書
def addBook(): Booklist=readBook() numlist=[] for book in Booklist: numlist.append(book["編號"]) newnum=max(numlist)+1 Bname=input("請輸入書名:") writer=input("請輸入圖書作者:") condi="可借" newBooklist={"編號": newnum, "書名": Bname, "作者": writer,"借出狀態":condi} Booklist.append(newBooklist) writeBook(Booklist)
4.刪除圖書
def delbook(): Booklist=readBook() Bnum=int(input("請輸入要刪除的圖書編號:")) Bname=input("請輸入要刪除的圖書名稱:") for book in Booklist: if Bnum==book["編號"] or Bname==book["書名"]: Booklist.remove(book) print("圖書已",book["書名"],"經刪除") writeBook(Booklist)
5.借閱圖書
def lendbook(): Booklist=readBook() Bnum=int(input("請輸入要借閱的圖書編號:")) Bname=input("請輸入要借閱的圖書名稱:") for book in Booklist: if Bnum==book["編號"] or Bname==book["書名"]: if book["借出狀態"]=="可借": print("此書在架,可借閱") a=int(input("借閱請安1,退出請按0:")) if a==1: book["借出狀態"]="已借出" else: break else: print("此書已被借出,等歸還後再借") writeBook(Booklist)
6.歸還圖書
def returnbook(): Booklist=readBook() Bnum=int(input("請輸入要還的圖書編號:")) Bname=input("請輸入要還的圖書名稱:") for book in Booklist: if Bnum==book["編號"] or Bname==book["書名"]: if book["借出狀態"]=="已借出": book["借出狀態"]="可借" print(book["書名"],"已還") else: print(book["書名"],"書已在架,不用歸還") writeBook(Booklist)
最後是調用所有模塊的總程序:
while 1==1: print("*************************圖書管理系統2.0******************************") msg=login() if msg=="成功": print("1.顯示所有的圖書;\n2.圖書上架;\n3.圖書下架;\n4.借閱;\n5.歸還;\n6.退出") print("---------------------------------------------------------------------") while 2==2: choice=int(input("請輸入業務編號(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("沒有此業務,請重新選擇") continue
這位是執行後的效果圖。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。