购物车要求:
用户名和密码存放于文件中 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额
#!/usr/bin/env python # -*- coding: utf-8 -*- ‘‘‘ 用户名和密码存放于文件中 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ‘‘‘ product_dic = { 1:[‘Iphone7‘,5800], 2:[‘Coffee‘,30], 3:[‘疙瘩汤‘,10], 4:[‘Python Book‘,99], 5:[‘Bike‘,199], 6:[‘ViVo X9‘,2499], } shop_list=[] shop_dic={} def shop(): Tag=True remain_Balance = Balance print("开始购物".center(30,"=")) for key,value in product_dic.items(): print(key,value) while Tag: serial_number=input("输入你要购买商品的编号>>:").strip() if serial_number.isdigit(): serial_number = int(serial_number) if serial_number > 6: print("请输入1-6") continue elif serial_number == "q": print("开始结算".center(20,"=")) for list in shop_list: product_name = list[0] product_price = list[1] shop_dic.setdefault(product_name, {}) shop_dic[product_name].setdefault("number", 0) shop_dic[product_name].setdefault("total", 0) shop_dic[product_name]["price"] = product_price if product_name in shop_dic: shop_dic[product_name]["number"] += 1 shop_dic[product_name]["total"] = shop_dic[product_name]["price"] * shop_dic[product_name]["number"] # 总价 total = 0 for product in shop_dic: print(product.center(7),str(shop_dic[product]["number"]).center(7),str(shop_dic[product]["total"]).center(5)) total = total + shop_dic[product]["total"] # if total > Balance: # print("余额不足购买这么多") break elif serial_number == "exit": print("直接退出") else: continue print("你要购买的商品编号{},商品{},价格{}".format(serial_number,product_dic.get(serial_number)[0],product_dic.get(serial_number)[1])) yes_no=input("输入y/n,确定加入购物车>>:") if yes_no == "y" : if product_dic.get(serial_number)[1] > remain_Balance: print("您的余额不够.无法加入到购物车,还差{}".format(product_dic.get(serial_number)[1]-remain_Balance)) else: shop_list.append(product_dic.get(serial_number)) remain_Balance = remain_Balance - product_dic.get(serial_number)[1] print(shop_list,remain_Balance) continue elif yes_no == "n": print(shop_list) pass continue elif yes_no == "exit": break else: print("非法输入,请输入y或者n") continue user_info={} while True: print(‘‘‘购物车小程序: 1、购物 2、注册账号 输入q退出 ‘‘‘) option=input("your option>>:").strip() if not option.isdigit(): print("input 1 or 2\n") continue option=int(option) if option > 2: print("input 1 or 2\n") continue if option == 1: Tag=True count=0 while Tag: input_user = input("your name>>:").strip() with open("user.txt", encoding="utf-8", mode="r") as read_f: for line in read_f: line=line.strip("\n") User=line.split(",")[0] Password=line.split(",")[1] Balance=line.split(",")[2] if input_user == User: user_info.setdefault(User,{}) user_info[User].setdefault("count",0) user_info[User]["Password"]=Password user_info[User]["Balance"]=Balance if user_info[User]["count"] >= 3: print("%s用户锁定" %(input_user)) Tag=False break else: print("{}用户不存在".format(input_user)) continue if Tag: input_password = input("your password>>:") if input_password == user_info[input_user]["Password"]: print("{}用户密码登陆正确".format(input_user)) print("用户:{},余额:{}".format(User, user_info[User]["Balance"])) ‘‘‘ 这时候才开始购物 ‘‘‘ Balance=int(Balance) shop() break else: user_info[input_user]["count"]+=1 print("{}用户密码登陆错误,还有{}尝试机会".format(input_user, 3 - user_info[input_user]["count"]))
原文地址:https://www.cnblogs.com/so-cool/p/8808983.html
时间: 2024-10-09 21:07:25