WELCOME

不积跬步,无以至千里;不积小流,无以成江海。

Python文件操作-图书管理系统

简易版图书管理系统

 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)

 

posted @ 2022-03-25 22:06  Ambitious~  阅读(83)  评论(0)    收藏  举报