首页 > 代码库 > 购物车程序

购物车程序

shoplist 可以自己修改喜欢的
技术分享

salary_yj 用户信息文件

技术分享



#
!/usr/bin/env python #打开文件加入到yj列表 with open("salary_yj","r",encoding="utf-8") as f: yj = eval(f.read()) #请用户输入用户名和密码,进行判断,并且显示出资金余额 while True: username = input("\033[34;42m请输入用户名: \033[0m") passwd = input("\033[34;42m请输入密码: \033[0m") if username == yj["name"] and passwd == yj["passwd"]: print("您的资金余额为:",yj["salary"]) else: print("用户名或密码错误") exit() #显示出资金后,问用户是否充值,然后进行判断y的话,用户输入金额+现有金额,写入文件 while True: pay = input("请问要充值吗? (y/n): ") if pay == "y": pay_rmb = input("请输入需要充值的金额: ") salary_new = yj["salary"] + int(pay_rmb) yj["salary"] = salary_new yj_pay_salary = (yj["salary"]) print("\033[45m现在余额为%d\033[0m" %(yj_pay_salary)) with open("salary_yj","w",encoding="utf-8") as ff: ff.write(str(yj)) #判断为n,打开shoplist,循环读取文件,并显示商品列表 elif pay == "n": with open("shoplist","r") as f: for i in f: print(i) #请求用户输入选择列表,当用户输入数字时,进行判断 while True: select = input("请选择购买商品编码,退出请按exit : ") with open("shoplist","r") as f_shop: for line in f_shop: #当用户选择商品编码等于循环文件的行数时,打印该行,如果选择exit,退出并打印用户购买的列表 if select == line[0]: new_line = line.split() print(new_line[1]) elif select == "exit": print(yj["list"]) exit() #同时判断用户的资金,如果小于该商品价值,打印余额不足,并退出该循环 with open("salary_yj","r",encoding="utf-8") as f: yj = eval(f.read()) if yj["salary"] < int(new_line[2]): print("您的余额不足: ") break else: #反之,用户余额减去商品价值,写入用户信息文件,并打印用户购买记录 salary_buy= yj["salary"] - int(new_line[2]) yj["salary"] = salary_buy yj["list"].append(new_line[1]) with open("salary_yj","w",encoding="utf-8") as f_new: f_new.write(str(yj)) print("\033[45m您的购买记录:\033[0m",yj["list"])

 

购物车程序