Python檔案操作-圖書管理系統
阿新 • • 發佈:2022-03-25
簡易版圖書管理系統
1 # 使用者註冊
2 import time
3
4
5 def register():
6 username = input('請輸入使用者名稱:')
7 password = input('請輸入密碼:')
8 repassword = input('請確認密碼:')
9
10 if password == repassword:
11 # 儲存資訊
12 with open(r'E:\book\user.txt', 'a') as wstream:
13 wstream.write(' {} {}\n'.format(username, password))
14 print('使用者註冊成功')
15 else:
16 print('密碼不一致')
17
18
19 def login():
20 username = input('請輸入使用者名稱:')
21 password = input('請輸入密碼:')
22
23 if username and password:
24 with open(r'E:\book\user.txt') as rstream:
25 while True:
26 user = rstream.readline()
27 if not user:
28 print('使用者名稱或密碼輸入錯誤!')
29 break
30 # print(user)
31 input_user = '{} {}\n'.format(username, password)
32 if input_user == user:
33 print('登入成功')
34 break
35
36
37 def show_books():
38 print('--------------圖書館裡面的圖書有-----------------')
39 with open(r'E:\book\books.txt', 'r', encoding='utf-8') as restream:
40 books = restream.readlines()
41 for book in books:
42 print(book, end='')
43
44
45 def borrow_book():
46 username = input('請輸入您的使用者名稱:')
47 book_name = input('請輸入您需要借閱的書籍名稱:')
48 with open(r'E:\book\books.txt', 'r', encoding='utf-8') as restream:
49 booklist = restream.readlines()
50 # print(type(booklist[1]))
51 for book in booklist:
52 book0 = book.strip('\n')
53 book = book0.split(' ')[0]
54 # number = book0.split(' ')[1]
55 # print(number)
56 if book_name == book:
57 print('借書成功')
58 with open(r'E:\book\user_books.txt', 'a') as wstream:
59 wstream.write(username)
60 wstream.write(':')
61 wstream.write(book_name)
62 wstream.write('\n')
63 # with open(r'E:\book\books.txt','a') as
64 break
65 else:
66 print('書籍不存在')
67
68
69 # 呼叫函式
70
71 while True:
72 print('\n1.使用者註冊\n2.使用者登入\n3.所有圖書\n4.借書')
73 n = input('請輸入您的操作:')
74 if n == '1':
75 register()
76 elif n == '2':
77 login()
78 elif n == '3':
79 show_books()
80 elif n == '4':
81 borrow_book()
82 else:
83 print('正在退出本系統')
84 time.sleep(3)
85 print('退出成功!!!感謝您的使用')
86 break
注意:特別注意strip()函式的應用(https://www.cnblogs.com/onebit123/p/16056793.html)