用Python做一个简单的图书管理系统
def intinput(n="请输入序号:"):
while True:
s=input(n)
if s.isnumeric():
return int(s)
else:
print("请输入整数")
books=[{"name":"Scratch","isbn":"12345","num":2},
{"name":"Python","isbn":"12346","num":5}]
print("""
***************************
* 欢迎进入图书管理系统 *
* 0 .退出 *
* 1 .列出所有书籍 *
* 2 .添加书籍 *
* 3 .修改书籍 *
* 4 .删除书籍 *
* 5 .借书 *
* 6 .还书 *
***************************
""")
while True:
n=input("输入序号(0-6):")
if n=="0":
print("退出成功")
break
elif n=="1":
print("序号 书名 书号 数量")
index=1
for book in books:
print("%d %s %s %d"%(index,book["name"],book["isbn"],book["num"]))
index+=1
elif n=="2":
book=dict()
book["name"]=input("请输入书名:")
book["isbn"]=input("请输入书号:")
book["num"]=intinput("请输入数量:")
books.append(book)
print("添加成功")
elif n=="3":
try:
xh=intinput()
book=books[xh-1]
book["name"]=input("请输入书名:")
book["isbn"]=input("请输入书号:")
book["num"]=intinput("请输入数量:")
print("修改成功")
except:
print("出错了")
elif n=="4":
s=intinput()
if 1<=s<=len(books):
del books[xh-1]
print("删除成功")
else:
print("超出范围")
elif n=="5":
xh=intinput()
book=books[xh-1]
if book["num"]>0:
book["num"]-=1
print("借书成功")
else:
print("库存不足")
elif n=="6":
xh=intinput()
try:
book=books[xh-1]
book["num"]-=1
print("还书成功")
except:
print("未查询到书本")
else:
print("输入错误,请重新输入")
print("")