购物车编写
Posted cellking-xdb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车编写相关的知识,希望对你有一定的参考价值。
实现购物车商品的增删:
#货物 货品名---价格 list_goods = [ ‘name‘:‘苹果‘,‘price‘: 9.9, ‘name‘:‘香蕉‘,‘price‘:2.23, ‘name‘:‘橘子‘,‘price‘:3.2, ‘name‘:‘香瓜‘,‘price‘:5, ‘name‘:‘西瓜‘,‘price‘:2.2, ‘name‘:‘火龙果‘,‘price‘:6, ‘name‘:‘李子‘,‘price‘:3, ]; user_list_goods = [ ] user_dic_goods = all_price = 0 print("欢迎光临!") money = input("付款>>>").strip() end_choose_flag = False if money.isdigit() and int(money) > 0: money_int = int(money) while not end_choose_flag: for i,good in enumerate(list_goods) : print("序号 商品: 价格".format(i,good[‘name‘],good[‘price‘])) good_series_num = input(‘请输入你要的商品序号,退出请按Q>>>‘) if good_series_num.upper() == ‘Q‘: break elif good_series_num.isdigit() and int(good_series_num) in range(0,len(list_goods)) : good_num = input(‘请输入你要的商品数量(如需删除/清空,请输入对应商品负数数量)>>>‘) price = list_goods[int(good_series_num)][‘price‘]*int(good_num) current_good_msg = ‘name‘:list_goods[int(good_series_num)][‘name‘], ‘msg‘:‘num‘:int(good_num), ‘price‘:price if price >= money_int - all_price :#余额不足的情况 while 1: choose = input("当前货品总价格为:,余额不足,是否充值?Y/N".format(all_price + price)) if choose.upper() == ‘Y‘: add_money = input("充值金额>>>") if add_money.isdigit() and int(add_money) > 0: money_int += int(add_money) print(‘money int , price :‘.format(money_int,price)) if price > money_int - all_price : continue else: all_price += price if current_good_msg[‘name‘] in user_dic_goods: user_dic_goods[current_good_msg[‘name‘]][‘num‘] += int( good_num) # ‘name‘:‘num‘,‘all_price‘ user_dic_goods[current_good_msg[‘name‘]][‘price‘] += price else: user_dic_goods.setdefault(current_good_msg[‘name‘], current_good_msg[‘msg‘]) print(user_dic_goods) break elif choose.upper() == ‘N‘: end = input("是否前去结算?Y/N") if end.upper() == ‘Y‘: end_choose_flag = True break elif end.upper() == ‘N‘: print("余额不足,无法继续操作,请充值!") else: print("您输入有误!") else: if int(good_num)>0: all_price += price if current_good_msg[‘name‘] in user_dic_goods: user_dic_goods[current_good_msg[‘name‘]][‘num‘] += int(good_num) #‘name‘:‘num‘,‘all_price‘ user_dic_goods[current_good_msg[‘name‘]][‘price‘] += price else: user_dic_goods.setdefault(current_good_msg[‘name‘],current_good_msg[‘msg‘]) print(user_dic_goods) print("当前购物车总值,余额 ".format(all_price,money_int - all_price)) else:#删除 if current_good_msg[‘name‘] in user_dic_goods: check_del = input("是否删除?对应数量商品?Y/N") if check_del.upper() == ‘Y‘: if int(good_num) < user_dic_goods[current_good_msg[‘name‘]][‘num‘]: user_dic_goods[current_good_msg[‘name‘]][‘num‘] += int(good_num) #‘name‘:‘num‘,‘all_price‘ user_dic_goods[current_good_msg[‘name‘]][‘price‘] += price else: check_clear = input("是否清空对应商品?Y/N") if check_clear.upper() == ‘Y‘: temp = user_dic_goods.pop(user_dic_goods[current_good_msg[‘name‘]]) print(user_dic_goods) print("当前购物车总值,余额 ".format(all_price, money_int - all_price)) else: print("您输入的数量有误,请重新输入") continue print(user_dic_goods) print("当前购物车总值,余额 ".format(all_price,money_int - all_price)) #user_list_goods.append(current_good_msg) else: print("输入有误,请重新输入!") #结算 print("当前清单:") for k,v in user_dic_goods.items(): print(" 数量 总值".format(k,v[‘num‘],v[‘price‘])) print("总计:,找余".format(all_price,money_int - all_price)) else: print("请给合法的纸币付款")
以上是关于购物车编写的主要内容,如果未能解决你的问题,请参考以下文章
如何从 recyclerview 片段传递到另一个 recyclerview 片段
VS Code配置snippets代码片段快速生成html模板,提高前端编写效率
我们可以在活动 xml 中编写 UI 以及在片段 xm 中编写 UI 吗?
如何使用spring webflux在功能性反应java中编写具有多个if else的复杂代码