# 配套程序,需要创建一个锁文件lock.txt,记录用户名密码的文件shopping_pw.txtuserSaveMoney = 0 # 定义全局变量,用户起始账户余额为0def Recharge(): # 充值函数 global userSaveMoney userInputMoney = int(input("请输入您要存入的金额:")) userSaveMoney += userInputMoney print("您充入的金额为:%d"%userSaveMoney)def Show(): # 商品陈列函数 global userSaveMoney print(""" 您的账号余额:%d 元 商品清单如下: -------------------------- 序号 商品名称 商品单价(元) -------------------------- 1 电脑 1999 2 鼠标 10 3 键盘 50 4 显示器 500 -------------------------- n 购物车结算 -------------------------- Q退出程序 -------------------------- """ % userSaveMoney)def Goods(): # 商品仓库函数 goodsList = [ # 商品仓库 { "num": 1, "name": "电脑", "price": 1999 }, { "num": 2, "name": "鼠标", "price": 200 }, { "num": 3, "name": "键盘", "price": 300 }, { "num": 4, "name": "显示器", "price": 1000 } ] return goodsListdef Shopping(goodsList): global userSaveMoney totalPrices = 0 shoppingCar = [] # 购物车列表,准备放置客户选购的商品 while True: Show() num = input("请输入您的操作(1-4)为选择添加到购物车中的商品:") if num in ["1", "2", "3", "4"]: # 判断是否是购买操作,输入1-4范围数字是购买操作 for el in goodsList: # 从商品仓库中找出用户选购的产品,并打印所选产品明细 if el["num"] == int(num): totalPrices += el["price"] shoppingCar.append(el) print(""" 您说选购商品信息如下: 商品名称:%s 商品单价:%d """ % (el["name"], el["price"])) elif num == "n": # 实现结算功能,判断用户账号余额情况 Balance(shoppingCar,totalPrices) elif num.upper() == "Q": # 实现退出程序功能 print("程序退出") BuiedGoods(shoppingCar,totalPrices) exit() else: print("您输入序号有误,请重新输入")def Balance(shoppingCar,totalPices,): global userSaveMoney print("*" * 50) print("购物车商品有:") for el in shoppingCar: # 打印购物车货物明细 print("%s %s %d" % (el["num"], el["name"], el["price"])) if totalPices <= userSaveMoney: # 判断购物车物品总金额是否超出用户账号金额 userSaveMoney -= totalPices print("交易完成") else: shoppingCarNum = [] for el in shoppingCar: # 为了下面程序判断用 shoppingCarNum.append(el["num"]) while True: # 该循环体实现用户余额不足的情况下处理逻辑 print("余额不足") select = input("输入x存值,输入产品序号从购物车删除商品:") if select == "x": # 利用充值的方式解决金额不足问题 userSaveMoney += int(input("请输入存入金额:")) if userSaveMoney > totalPices: userSaveMoney -= totalPices print("交易完成") break elif int(select) in shoppingCarNum: # 利用删除购物车商品的方式解决金额不足问题 count = 0 for el in shoppingCar: if el["num"] == int(select): shoppingCar.pop(count) totalPices -= el["price"] count += 1 if userSaveMoney > totalPices: # 商品减少的情况下再次判断账号余额是否充足 userSaveMoney -= totalPices print("交易完成") break else: print("输入有误,请重新输入")def BuiedGoods(shoppingCar,totalPrices): global userSaveMoney print("您购买商品明细如下:") # 结算完成后显示购买信息,消费总计,账号余额。 for el in shoppingCar: print("%s %s %s" % (el["num"], el["name"], el["price"])) print(""" 共计消费%d元, 账户余额%d元 """ % (totalPrices, userSaveMoney)) def Buy(): print(""" 您的账号余额为%d元,是否充值 1.充值 2.购物 """%userSaveMoney) select = input("请输入操作选项:") if select == "1": Recharge() Buy() elif select == "2": Shopping(Goods()) # 将商品添加到购物车# 定义注册函数def register(): # 单个用户信息保存在字典里,再将每个人的信息字典放在文件中 userInfo = {} username = input("请输入用户名:").strip() pw = input("请输入登录密码").strip() pw1 = input("请再次输入密码").strip() if pw == pw1: with open("./shopping_pw.txt",mode="a+",encoding="utf-8") as f: # shopping_pw.txt用于保存用户名和密码信息的文件 for line in f: dic = dict(line) if username == dic["name"]: print("用户名已存在,请修改") register() else: userInfo["name"] = username userInfo["password"] = pw f.write(str(userInfo)+"\n") print("注册成功") logIn() else: print("两次输入密码不一致,请重新输入")#2.定义登录函数def logIn(): # 构建登录函数 count = 4 f = open("lock.txt", "r") lock = int(f.read()) f.close() if lock == 1: print("您的账户已锁定,请联系您的管理员解锁") else: # 2.提示用户输入用户名和密码 # print("请输入您的用户名和密码") print("以下是登录界面,您只有三次重试机会,输错账户将锁定,请慎重输入") while True: print("*"*100) print("进入登录页面") print("*"*100) usernameInput = input("请输入用户名:").strip() userpwInput = input("请输入密码:").strip() with open("./shopping_pw.txt", mode="r", encoding="utf-8") as f: for line in f: info = eval(line) # 还原字典类型 if info["name"] == usernameInput and info["password"] == userpwInput: print("验证成功,欢迎登录") Buy() break else: count -= 1 if count <= 0: print("您已经重试三次,账户被锁定,请联系管理员解锁") fileLock = open("lock.txt", "w") # 重试三次后,设置lock锁文件 fileLock.write("1") fileLock.close() break else: print("您还有%d重试机会,请确保输入正确,否则账户将被锁定" % count) # print("您输入的用户名密码有误,请重新输入") # logIn()#1.定义功能函数def func(select): if select == "1": logIn() elif select == "2": register() elif select == "3": Buy() elif select == "4": print("退出程序") exit() else: print("您输入的选项不存在,请重新输入")def main(): select = input(""" ------1.登录------ ------2.注册------ ------3.购物------ ------4.退出------ 请选择要进行的操作的序号: """) func(select)main()
原文地址:https://www.cnblogs.com/yyl668/p/9419347.html
时间: 2024-10-08 08:21:16