实现功能有:
输入账号,密码。
根据账号密码会判断为买家还是卖家,或者是已经被冻结的账号(密码输入三次错误账号就将被冻结)。
买家入口:
1.根据序号购买商品
2.每次购买完成,都会显示本次购买的商品以及余额
3.若余额不足,会提示购买失败
4.按q键可退出
5.最后会显示出购买的所有商品和余额
卖家入口:
1.添加商品
2.删除商品
3.修改商品
4.按q键可退出
注:买家信息、卖家信息、商品信息都是写在excel表格中的。冻结账户保存在txt文件中。
import xlrd
socklist = []
customer_list = []
business_list = []
goods_list = []
boughtgoods_list = []
money_flag = True
choose_flag =True
socket = open("C:\\Users\\user\\Desktop\\python学习\\Study\day5\\socket.txt")
for line in socket:
socklist.append(line.strip("\n"))
xl_1 = xlrd.open_workbook("C:\\Users\\user\\Desktop\\python学习\\Study\day5\\customerinfo.xlsx")
sheet_1 = xl_1.sheet_by_index(0)
for row in range(sheet_1.nrows):
new_customer_list = sheet_1.row_values(row)
customer_list.extend([new_customer_list])
xl_2 = xlrd.open_workbook("C:\\Users\\user\\Desktop\\python学习\\Study\day5\\businessinfo.xlsx")
sheet_2 = xl_2.sheet_by_index(0)
for row in range(sheet_2.nrows):
new_business_list = sheet_2.row_values(row)
business_list.extend([new_business_list])
xl_3 = xlrd.open_workbook("C:\\Users\\user\\Desktop\\python学习\\Study\day5\\goodsinfo.xlsx")
sheet_3 = xl_3.sheet_by_index(0)
for row in range(sheet_3.nrows):
new_goods_list = sheet_3.row_values(row)
goods_list.extend([new_goods_list])
for i in range(3):
print("您好,请登录!")
username = input("请输入您的账号:")
password = input("请输入您的密码:")
if(username in socklist):
print("对不起,您的账号已经被冻结,无法使用!")
print("\n")
break
elif ((username==business_list[0][0])and(password==business_list[0][1])) or ((username==business_list[1][0])and(password==business_list[1][1])):
msg = '''------欢迎老板{name}------'''.format(name=username)
print(msg)
print("商品展示如下:")
for j in range(len(goods_list)):
print((j + 1), ".", goods_list[j][0], ":", goods_list[j][1])
print("以下是可以办理的业务:")
print("1.增加商品")
print("2.删除商品")
print("3.修改商品")
while choose_flag:
choose1 = input("请选择您所办理的业务:")
if(choose1=="q"):
print("\n")
break
else:
choose = int(choose1)
if (choose == 3):
modifiednumber = input("请输入您要修改商品的编号:")
modifiednumber2 = int(modifiednumber)
print("您要修改的商品为:", goods_list[modifiednumber2 - 1])
price = input("请输入您要修改的价格:")
goods_list[modifiednumber2 - 1][1] = price
print("恭喜您,您已修改商品成功!")
print("商品展示如下:")
for j in range(len(goods_list)):
print((j + 1), ".", goods_list[j][0], ":", goods_list[j][1])
print("以下是可以办理的业务:")
print("1.增加商品")
print("2.删除商品")
print("3.修改商品")
continue
elif (choose == 2):
modifiednumber = input("请输入您要删除商品的编号:")
modifiednumber2 = int(modifiednumber)
print("您要删除的商品为:", goods_list[modifiednumber2 - 1])
confirm = input("确定要删除吗?(yes/no):")
if (confirm == "yes"):
goods_list.remove(goods_list[modifiednumber2 - 1])
print("恭喜您,您已删除商品成功!")
print("商品展示如下:")
for j in range(len(goods_list)):
print((j + 1), ".", goods_list[j][0], ":", goods_list[j][1])
print("以下是可以办理的业务:")
print("1.增加商品")
print("2.删除商品")
print("3.修改商品")
continue
else:
pass
elif (choose == 1):
added_good_name = input("请输入您要添加的商品名称:")
added_good_price = input("请输入您要添加的商品价格:")
print("恭喜您,您已添加商品成功!")
goods_list.append([added_good_name, added_good_price])
print("商品展示如下:")
for j in range(len(goods_list)):
print((j + 1), ".", goods_list[j][0], ":", goods_list[j][1])
print("以下是可以办理的业务:")
print("1.增加商品")
print("2.删除商品")
print("3.修改商品")
continue
elif ((username==customer_list[0][0])and(password==customer_list[0][1])) or ((username==customer_list[1][0])and(password==customer_list[1][1])) or ((username==customer_list[2][0])and(password==customer_list[2][1])):
msg = '''------欢迎顾客{name}------'''.format(name=username)
print(msg)
print("商品展示如下:")
for j in range(len(goods_list)):
print((j + 1), ".", goods_list[j][0], ":", goods_list[j][1])
money = int(input("请输入您的总额:"))
while money_flag:
choosednumber = input("请输入您想要的商品序号:")
if choosednumber == "q":
break
else:
choosednumber2 = int(choosednumber)
price = int(goods_list[(choosednumber2 - 1)][1])
if money >= price:
extramoney = money - int(goods_list[choosednumber2 - 1][1])
money = extramoney
boughtgoods_list.append(goods_list[choosednumber2 - 1][0])
print("恭喜您,您已成功拥有了:",goods_list[choosednumber2 - 1][0])
else:
print("对不起,您的余额不足!")
print("所剩余额:", money)
print("所购买商品为:", boughtgoods_list, "所剩余额为:", money)
print("\n")
else:
if(i<2):
print("对不起,您的账号或密码错误,请重新登录!")
continue
elif(i==2):
print("对不起,您错误次数太多,您的账号已经被冻结!")
w = open("C:\\Users\\user\\Desktop\\python学习\\Study\day5\\socket.txt", 'a')
w.write(username + "\n")
break
本小白还木有学习方法和类...
所以代码只能酱紫(确实有些low)~
等后面学习了方法、类接口再更新修改~加油~