注册函数:
def regisiter(): with open(r"F:\pylearn\day12\购物车系统\账号密码.txt","r",encoding="utf8") as fr: user_dict=dict() data=fr.read() for i in range(len(data.split())): user_dict[data.split()[i].split("|")[0]]=data.split()[i].split("|")[1] #egi_count=0 while True: user_regi=input("注册:请输入你的用户名:>>>>") if user_regi not in user_dict: pwd_regi=input("注册:请输入你的密码:>>>>") pwd_regi_again=input("请再次输入你的密码:>>>>") if pwd_regi==pwd_regi_again : with open(r"F:\pylearn\day12\购物车系统\账号密码.txt", "a", encoding="utf8") as fw: fw.write(f"\n{user_regi}|{pwd_regi}") print("注册成功!") break else: print("两次输入的密码不一致,请重新再试!") else: print("此用户名已被注册!请更换一个用户名!") #regi_count+=1View Code
登陆函数:
def logging(): logging_count=0 with open(r"F:\pylearn\day12\购物车系统\账号密码.txt","r",encoding="utf8") as fr: user_dict=dict() data=fr.read() for i in range(len(data.split())): user_dict[data.split()[i].split("|")[0]]=data.split()[i].split("|")[1] print(user_dict) print("你一共有3次输入账号密码的机会!") while logging_count<3: username=input("请输入你的用户名:>>>") pwd=input("请输入你的密码:>>>") if username in user_dict and pwd==user_dict.get(username): print("恭喜您,登陆成功!") break else: print("登陆失败请重新再试!") logging_count+=1 print(f"你现在还有{3-logging_count}次机会") if logging_count==3: print("次数已用尽,不允许再登陆!")View Code
账户余额查询函数:
def salary_account(): with open(r"F:\pylearn\day12\购物车系统\账户余额.txt","r",encoding="utf8") as fr: salary=int(fr.read()) #print("您的账户余额为:\n",salary) return salaryView Code
商品列表查询函数:
def sale_display(): sale_dict=dict() with open(r"F:\pylearn\day12\购物车系统\商品列表.txt","r",encoding="utf8") as fr: sale_data=fr.read() for i in range(len(sale_data.split())): sale_dict[sale_data.split()[i].split(":")[0]]=sale_data.split()[i].split(":")[1] #print("商品列表如下:\n",sale_dict) return sale_dictView Code
buy函数():
import sale_display import salary_account def buy(): with open(r"F:\pylearn\day12\购物车系统\已购买商品.txt", "w", encoding="utf8") as fa: fa.write("已购列表:\n") money=salary_account.salary_account() sale_dict=sale_display.sale_display() #print("商品列表如下:\n", sale_dict) buy_flag=True while buy_flag: print("商品列表如下:\n", sale_dict) money = salary_account.salary_account() print(f"您的余额为:{money}") print("*"*120) target=input("请输入你想要买的东西:>>>") print("*" * 120) if target in sale_dict: price=int(sale_dict.get(target)) print(f"{target}的单价为:{price}") print("*" * 120) num=int(input("请输入你购买的数量:>>>")) print("*" * 120) confim=input(f"确认购买吗?一共需要花费{num*price}块大洋!确认请按y,取消请按n:\n").strip() print("*" * 120) if confim=="n": print("欢迎下次光临!") print("*" * 120) if confim=="y" and money>price*num: #print("购买成功!") with open(r"F:\pylearn\day12\购物车系统\账户余额.txt","w",encoding="utf8") as fw: fw.write(f"{money-price*num}") print(f"恭喜您成功购买{num}件{target}") print("*" * 120) #salary_account.salary_account() money = salary_account.salary_account() print(f"您的余额为:{money}") print("*" * 120) with open(r"F:\pylearn\day12\购物车系统\已购买商品.txt","a",encoding="utf8") as fa: fa.write(f"商品名:{target} 数量:{num}\n") buy_again=input("是否选择继续购买,继续请按y,结束请按n\n") print("*" * 120) if buy_again=="y": print("请继续选购!") print("*" * 120) #print("商品列表如下:\n", sale_dict) #print("*" * 120) if buy_again=="n": buy_flag=False print("欢迎下次再来!") print("*" * 120) with open(r"F:\pylearn\day12\购物车系统\已购买商品.txt","r",encoding="utf8") as fr: buy_list=fr.read() print("以下是已购商品列表\n",buy_list) if confim == "y" and money < price*num: print("余额不足,请充值!") print("*" * 120) insert_money=input("是否选择充值?充值请输入y,不充值请输入n:\n") print("*" * 120) if insert_money=="y": inset_money_num=int(input("请输入充值数量:>>>")) print("*" * 120) with open(r"F:\pylearn\day12\购物车系统\账户余额.txt","w",encoding="utf8") as fw: fw.write(f"{money+inset_money_num}") money = salary_account.salary_account() #print(f"您的余额为:{money}") #print("*" * 120) if insert_money=="n": print("先去赚钱!") print("*" * 120) buy_flag = False with open(r"F:\pylearn\day12\购物车系统\已购买商品.txt", "r", encoding="utf8") as fr: buy_list = fr.read() print("以下是已购商品列表\n",buy_list) break else: print("您输入的商品不存在!请重新输入!") #buy()View Code
main函数:
#用户名和密码存放于文件中 # 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 # 允许用户根据商品编号购买商品 # 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 # 可随时退出,退出时,打印已购买商品和余额 import regisiter import loggin import buy regi_flag=input("是否拥有账号?没有账号请输入n开始注册,否则按其他任意键开始登陆!>>>\n") if regi_flag=="n": regisiter.regisiter() buy.buy() else: loggin.logging() buy.buy()
标签:实战,buy,money,sale,购物车,120,dict,day12,print From: https://www.cnblogs.com/yyyjw/p/17856099.html