python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果。# 1.客户输入相应序号和对应数量后将该商品加入购物车# 2.付款时检查客户是否有支付能力(结算金额<=100)# 3.客户输入Q/q退出选购
lis = [{‘name‘: ‘苹果‘, ‘price‘: 8}, {‘name‘: ‘香蕉‘, ‘price‘: 5}, {‘name‘: ‘芒果‘, ‘price‘: 15}, {‘name‘: ‘葡萄‘, ‘price‘: 12}] moeny = input(‘请输入充值金额:‘) for i, v in enumerate(lis): print(‘序号:{},商品:{},价格:{}‘.format(i + 1, v[‘name‘], v[‘price‘])) shopping = {} flag = True while flag: chooes = input(‘请输入选择的商品序号(输入Q/q退出):‘) if chooes.isdigit() and int(chooes) <= len(lis): while 1: unmber = input(‘请输入需要购买的数量(输入Q/q退出):‘) if unmber.isdigit(): name = lis[int(chooes) - 1][‘name‘] v = 0 if shopping.get(name, ‘no‘) == ‘no‘: shopping[name] = unmber else: v = int(shopping[name]) unmber = int(unmber) + v shopping[name] = unmber print(‘---添加购物车成功---‘) print(‘您购买的是:{},数量为:{}‘.format(name, int(unmber) - v)) see_shopping = input(‘---输入Y/y/1查看购物车或结算,其他任意键继续选购---‘) if see_shopping == ‘Y‘ or see_shopping == ‘y‘ or see_shopping == ‘1‘: for name, number in shopping.items(): print(‘您的购物车里面有:{},数量:{}‘.format(name, number)) #未进行结算 else: break break elif unmber == ‘Q‘ or unmber == ‘q‘: flag = False break else: print(‘输入有误,请重新输入。‘) elif chooes == ‘Q‘ or chooes == ‘q‘: break else: print(‘输入有误,请重新输入。‘) print(‘---程序已退出---‘)
原文地址:https://www.cnblogs.com/zhuzl/p/11135303.html
时间: 2024-10-07 06:12:43