A python book management system based on halved search
Recently, the homework left by the teacher made my mind go through a storm
, experienced the damage of countless bugs, and after struggling countless times in my heart, I
decided to share this sad "story" and code with you.
Our group decided to design a book management system, which made me hug my weak body to comfort myself on countless cold nights~~~, looking at the topics of the former bigwigs, I feel so powerful, [weak]. jpg~
Okay, no hypocrisy, although the homework is not over and the topic is not over, but I still want to send the code to discuss with you, there are also many bugs, I will also bring up a few, I hope that after the big guys find out Point it out in the comments
Okay, without further ado, it's just code
#序号
id=['1','2','3','4']
#书名
books=['老人与海','瓦尔登湖','狂人日记','堂吉诃德']
#作者
author=['海明威','亨利.梭罗','鲁迅\t','塞万提斯']
#价格
prices=['29','28','44','62']
#库存
stocks=['25','19','38','21']
#用户名和密码列表
user=[['xiaoming','666'],['xiaozhang','666']]
#登录函数
def denglu():
d1=input("请输入用户名:")
d2=input("请输入密码:")
if [d1,d2] in user:
ui()
global b
tag=0
else:
print("输入错误,请重新登录!")
#注册函数
def zhuce():
dd1=input("请输入要注册的用户名:")
dd2=input("请输入要注册的密码:")
user.append([dd1,dd2])
print("注册成功!")
#最先开始的函数
tag=1
def main():
global tag
while tag==1:
print("*************************************")
print(" 欢迎来到图书管理信息系统 ")
print("*************************************")
print(" 1.登 录 ")
print(" 2.注 册 ")
print(" 3.退 出 ")
print("*************************************")
ss = input("请选择您的操作:")
if int(ss) ==1:
denglu()
elif int(ss)==2:
zhuce()
else:
tag=0
#展示所有图书函数
def showall():
print('序 号', '\t\t','书 名','\t\t','作者','\t\t', '价 格','\t\t', '库 存')
for i in range(len(id)):
print('',id[i],'\t ',books[i],'\t\t',author[i],'\t ',prices[i], '\t\t ',stocks[i])
#图书入库函数
def ruku():
cc = 0
a1 = input("请输入想要入库的图书序号:")
def BinarySearch(id,item):
n = len(id)
if a1 in id:
mid = n // 2
if id[mid] == item:
a5 = int(input("请输入想要入库的图书库存"))
temp = int(stocks[mid]) + a5
stocks[mid] = str(temp)
mergeSort(id)
elif item < id[mid]:
return BinarySearch(id[:mid],item)
else:
return BinarySearch(id[mid+1:],item)
print("录入成功!")
else:
a2 = input("请输入想要入库的图书名字")
a3 = input("请输入想要入库的图书作者:")
a4 = input("请输入想要入库的图书价格:")
a5 = input("请输入想要入库的图书库存:")
id.append(a1)
books.append(a2)
author.append(a3)
prices.append(a4)
stocks.append(a5)
for i in range(len(id)):
print('',id[i],'\t ',books[i],'\t\t',author[i],'\t\t ',prices[i], '\t\t ',stocks[i])
print("录入成功!")
#调用折半查找函数
BinarySearch(id,a1)
#图书借出函数
def borrowing():
b1 = int(input("请输入需要借出的图书序号:"))
def BinarySearch2(id,item):
n = len(id)
if n > 0:
mid = n // 2
if int(id[mid]) == item:
temp1 = int(stocks[mid]) - 1
stocks[mid] = str(temp1)
mergeSort(id)
#for i in range(len(id)):
#print('',id[i],'\t ',books[i],'\t\t',author[i],'\t\t ',prices[i], '\t\t ',stocks[i])
print("图书借出成功!")
elif item < int(id[mid]):
return BinarySearch2(id[:mid],item)
else:
return BinarySearch2(id[mid+1:],item)
else:
print("没有你搜索的图书哦")
BinarySearch2(id,b1)
#将图书排序(归并排序)
def mergeSort(id):
if len(id) == 0 or len(id) == 1:
return id
mid = len(id) // 2
left = id[:mid]
right = id[mid:]
sortedLeft = mergeSort(left)
sortedRight = mergeSort(right)
return merge(sortedLeft,sortedRight)
def merge(left,right):
result = []
while len(left) >0 and len(right) >0:
if left[0] <= right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
result += right
result += left
i = 0
print('序 号', '\t\t','书 名','\t\t','作者','\t\t', '价 格','\t\t', '库 存')
while i < len(id):
print('',id[i],'\t ',books[i],'\t\t',author[i],'\t\t ',prices[i], '\t\t ',stocks[i])
i = i + 1
#退出函数
tag1=1
def exit():
global tag1
tag1=0
print("成功退出图书管理信息系统!")
#图书管理系统主界面函数
def ui():
while tag1==1:
print("*************************************")
print(" 欢迎使用图书管理信息系统 ")
print("*************************************")
print(" 1.查看图书 ")
print(" 2.图书入库 ")
print(" 3.图书借出 ")
print(" 4.退 出 ")
print("*************************************")
control=int(input("请选择您的操作:"))
if control==1:
showall()
elif control==2:
ruku()
elif control==3:
borrowing()
elif control==4:
exit()
else:
print("输入错误,请重新输入!")
main()
A word about bugs:
I listed three bugs (a sad thing), if you know how to solve it, please correct me in the comment area [please]
555~~~~
A picture is attached at the end:
I wish you all your wishes in the coming June [I also wish me not to fail the subject and show not too bad! ], luckily~~~