第一章作业购物车程序
数据结构: goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ...... ] 功能要求: 基础要求: 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表 2、允许用户根据商品编号购买商品 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4、可随时退出,退出时,打印已购买商品和余额 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] shopping_cart = [] user_name = input("user_name") password = input("password") while True: # 判断输入的薪水是不是数字类型 salary = input("salary") if salary.isdigit(): salary = float(salary) print("商品列表".center(30, "-")) for index, g in enumerate(goods): print(index, g) break else: print("薪水应为数字类型,请重新输入") while True: choice = input("请输入想购买的商品编号,退出请输q:") if choice.isdigit(): choice = int(choice) if 0 <= choice < len(goods): if salary > goods[choice].get("price"): shopping_cart.append(goods[choice]) salary -= goods[choice].get("price") print("商品", goods[choice].get("name"), "已加入购物车") else: print("余额不足,请重新挑选商品") else: print("商品编号有误,请重新输入") elif choice == "q": if len(shopping_cart) != 0: print("您已购买以下商品,余额为", salary,) for index, g in enumerate(shopping_cart): print(g) else: print("您没有购买任何商品") break else: print("商品编号有误,请输入整数类型编号")