要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
思路:
1、index,和for循环商品列表
2、商品号要在商品序列内
3、判断余额
4、打印所选商品及价格
goods =[ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] shopping_list = [] wage = input(‘请输入你的工资:‘) if wage.isdigit(): wage=int(wage) while True: for index,i in enumerate(goods): print(index,i) user_choice = input(‘请选择要买的编号>>:‘) if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(goods) and user_choice >=0: goods_item = goods[user_choice] print(goods_item[‘price‘]) if goods_item[‘price‘] < wage:#买的起 shopping_list.append(goods_item) wage -=goods_item[‘price‘] print("添加 %s 到你的购物车,你的余额为 \033[31;1m%s\033[0m" %(goods_item,wage) ) else: print("\033[41;1m你的余额不足[%s]啦\033[0m" % wage) else: print("product code [%s] is not exist!"% user_choice) elif user_choice == ‘q‘: print("--------shopping list------") for p in shopping_list: print(p) print("你的余额为:",wage) exit() else: print("invalid option")
原文地址:https://www.cnblogs.com/wangkun-251938485/p/8297211.html
时间: 2024-10-30 07:12:35