PythonDay5
程序员文章站
2022-03-16 08:55:31
...
每日一记
今天老师把昨天给我们留的图书管理系统带着我们做了一下
下面的代码是定义增删改查和登陆程序的函数:
import xlrd
from xlutils.copy import copy
import hashlib
def login(username,password):
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("users")
r = sheet.nrows
c = sheet.ncols
'''
m 代表username所在的列下标
p 代表password所在的列下标
q 代表state所在的列下标
'''
flag = False
m = 0
p = 0
q = 0
for j in range(c):
if sheet.cell(0,j).value == "username":
m = j
elif sheet.cell(0,j).value == "password":
p = j
elif sheet.cell(0, j).value == "state":
q = j
password = hashlib.md5(password.encode("utf-8")).hexdigest()
for i in range(1,r):
if sheet.cell(i,q).value == "0" and sheet.cell(i,m).value == username and sheet.cell(i,p).value == password:
flag = True
break
return flag
def showbook():
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
'''
m 代表username所在的列下标
p 代表password所在的列下标
q 代表state所在的列下标
'''
q = 0
for j in range(c):
if sheet.cell(0, j).value== "state":
q = j
for i in range(1,r):
if sheet.cell(i,q).value == "1":
continue
for k in range(c):
if k == q:
continue
print(sheet.cell(i,k).value,end=" ")
print()
def addBook(a):
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
for i in range(c):
try:
sheet_copy.write(r,i,a[i])
except:
break
book_copy.save("E://book.xls")
def updateBook(a):
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
cr = 0 #要修改的行号
for i in range(r):
if sheet.cell(i,0).value == a[0]:
cr = i
break
for i in range(c):
try:
sheet_copy.write(cr,i,a[i])
except:
break
book_copy.save("E://book.xls")
def deleteBook(id):
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
cr = 0 # 要修改的行号
for i in range(r):
if sheet.cell(i, 0).value == id:
cr = i
break
sheet_copy.write(cr,c-1,"1")
book_copy.save("E://book.xls")
然后是进入这个管理系统,并实现一系列的操作,代码如下:
import text190118 as book
import hashlib
import xlrd
from xlutils import copy
print("-----------欢迎登陆图书管理系统---------")
count = 0
while count < 3:
username = input("请输入用户名:")
password = input("请输入密码:")
result = book.login(username,password)
if result:
while True:
print("1-查看图书信息")
print("2-添加图书信息")
print("3-修改图书信息")
print("4-删除图书信息")
print("5-退出")
op = input("请输入操作编号:")
if op == "1":
book.showbook()
elif op == "2":
a = input("请输入图书编号:")
b = input("请输入图书名字:")
c = input("请输入图书出版社:")
d = input("请输入图书价格:")
e = input("请输入图书作者:")
book_ = [a,b,c,d,e,"0"]
book.addBook(book_)
elif op == "3":
a = input("请输入图书编号:")
b = input("请输入图书名字:")
c = input("请输入图书出版社:")
d = input("请输入图书价格:")
e = input("请输入图书作者:")
book_ = [a,b,c,d,e]
book.updateBook(book_)
elif op == "4":
id = input("请输入图书编号:")
book.deleteBook(id)
elif op =="5":
break
else:
print("请输入正确编号")
break
else:
print("密码错误")
count +=1
if count==3:
book = xlrd.open_workbook("E://book.xls")
sheet = book.sheet_by_name("users")
r = sheet.nrows
c = sheet.ncols
book_copy = copy.copy(book)
sheet_copy = book_copy.get_sheet(0)
for i in range(1,r):
if sheet.cell(i,1).value == username:
sheet_copy.write(i,3,"1")
break
print("您的账户已经被锁定")
book_copy.save("E://book.xls")
昨天说到了函数的形式
函数的构成:
def 函数名(参数列表):
函数体
return 返回值
函数是有参数的,下面是函数的几个参数
函数的参数
1,必须参数
2,默认参数 a = 9
3,关键字参数
4,不定长参数 *a **a
argument(实参) parameter(形参)
注意:
顺序必须是: 必须参数 默认参数/不定长参数 (必须参数在前,默认参数在后)
def asd(name="李四",age=18):
print(name,age)
asd(age =20,name ="张三")
显示结果如下
张三 20
不定长参数可以传递多个参数 用*参数 表示
def showInfo(*a):
print(a)
showInfo("张三丰",180,"男")
结果如下
('张三丰', 180, '男')
参数的顺序
def s(*c, a, b=12):
print(a, '---' , b ,'---' , c)
s(1, 2, 3, 4, 5, 6, a=7)
结果
7 --- 12 --- (1, 2, 3, 4, 5, 6)
下一篇: pythonday2