Realization of additions and deletions Cart:
# Goods name goods --- price list_goods = [ { 'name': 'apple', '. Price':} 9.9, { 'name': 'banana', '. Price': 2.23}, { 'name': 'orange ','. price ':} 3.2, {' name ':' melon ','. price ':. 5}, {' name ':' watermelon ','. price ':} 2.2, {' name ':' dragon ' , '. price':. 6}, { 'name': 'plum', '. price':. 3}, ]; user_list_goods = [ ] user_dic_goods = { } all_price = 0 Print ( "! Welcome") Money = INPUT ( " payment >>> ") 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 ( 'Enter the product number you want to exit the press >>> Q ') IF good_series_num.upper () ==' Q ': BREAK elif good_series_num.isdigit () and int (good_series_num) in the Range (0, len (list_goods)): good_num the iNPUT = (' Please input number of items (to delete / empty, enter the number corresponding to the commodity negative) >>> ') . 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: # insufficient balances while 1: 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 the 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 ( "! insufficient funds, can not continue, please recharge") the else: Print ( "You make a mistake!") the 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'])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 ( "Delete? corresponds to the number of goods? 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 ( "gross current shopping cart {}, the balance of {}" format (all_price, money_int Print (user_dic_goods) the else: Print ( " the number you entered is incorrect, please re-enter ") the Continue Print (user_dic_goods) Print (" gross current shopping cart {}, the balance of {} "format (all_price, money_int -. all_price)) # user_list_goods.append (current_good_msg) the else : print ( "input is incorrect, please re-enter!") # settlement print ( "current inventory:") for k,v in user_dic_goods.items(): print ( "{} {} total number of {}" the format (K, V [ 'NUM'], V [ '. price']).) Print (. "Total: {}, find I {}" format (all_price , money_int - all_price)) the else: Print ( "Please give legal bill payment")