• yanyu-xin
    2023-02-04 来自广东
    # 库存: {商品1编码:[数量,库存位置编码1]} stocks = {1000:[6, 1234], 1001:[3, 2345], 1002:[20,1356]} def clean_Cart(): """ 清空购物车""" for product_id, p_num in cart.items(): cart_stocks(p_id, p_num) # 购物车放回库存 cart.clear() # 清空购物车 return def cart_stocks(p_id, p_num): """ 购物车商品返回库存""" for i in range(p_num) : stocks_add(p_id) return def stocks_del(p_id): """ 库存减少一件商品""" stocks[p_id][0] -= 1 return def stocks_add(p_id): """ 库存增加一件商品""" if p_id in stocks : # 库存有 stocks[p_id][0] += 1 else : # 库存没 iof = [1, 4567] # 新入库商品数量为1,存放位置编码4567 stocks.setdefault(p_id,iof) return def stocks_produce(p_id,p_num) : """ 当商品库存为0,或不存在,返回False""" if p_id in stocks: # 库存有 num1 = stocks[p_id][0] if (num1 < p_num) or (num1 == 0) : # 库存数量对否 return False return True # 有库存,数量足够 else : # 库存资料没有找到 return False def add_Products(p_id,p_num): """ 购物车增加产品,库存减少产品 """ if stocks_produce(p_id,p_num) : # 判断准备减少的库存商品、数量是否正确 for product in range(p_num): # 逐一增加减少 addProduct(p_id) # 购物车增加一件 stocks_del(p_id) # 库存减少一件 else: print(f'减少库存的商品出错。商品:{p_id} ,数量:{p_num}') return def car_produce(p_id,p_num): """ 减少的购物车商品不在购物车或数量多于购物车为False""" if (p_id in cart) : if cart[p_id] >= p_num: return True return False def del_Products(p_id,p_num): """ 购物车减少产品,库存增加产品""" if car_produce(p_id,p_num) : # 减少的购物车商品、数量是否正确 for product in range(p_num): delProduct(p_id) # 购物车减少 stocks_add(p_id) # 库存增加 else: print(f'减少购物车出错。商品:{p_id} ,数量:{p_num}') return
    展开
    
    1
  • Matthew
    2022-12-16 来自江苏
    # 商品 products = [[1000, "iphone", "phone", 12000], [1001, "ipad", "pad", 15000], [1002, "macbook", "laptop", 20000]] # 购物车 "product_id":"product_number" cart = {1000:5, 1001:2} # 库存 stocks = {1000:10, 1001:20, 1002:30} def idtoname(product_id): """ product_id to product_name """ for product in products: if product[0] == product_id: return product[1] break def idtoprice(product_id): """ product_id to product_price """ for product in products: if product[0] == product_id: return product[3] break def showCart(): """ 展示购物车里的商品 """ totalMoney = 0 for product in cart: print(f"购买商品 { idtoname(product) } 的数量为 {cart[product]}") totalMoney += idtoprice(product)*cart[product] print(f"总费用为: {totalMoney}") # 修改增加商品函数的代码 def addProduct(product_id, product_number): """增加新商品,或已有商品数量""" if product_number <= stocks[product_id]: if product_id in cart.keys(): cart[product_id] += product_number else: cart[product_id] = product_number stocks[product_id] -= product_number print(f"商品 {idtoname(product_id)} 成功添加进购物车 {product_number} 个!") else: print(f"商品 {idtoname(product_id)} 库存不足,无法添加!") # 修改减少商品函数的代码 def delProduct(product_id): """减少已有商品数量""" if product_id in cart.keys(): cart[product_id] -= 1 if cart[product_id] == 0: cart.pop(product_id) stocks[product_id] += 1 print(f"购物车中的商品 {idtoname(product_id)} 成功减少 1 个!") else: print(f"商品编号 {idtoname(product_id)} 在购物车中不存在,无法删减!") def cleanCart(): """清空购物车""" cart.clear()
    展开
    共 1 条评论
    1
  • acondess
    2023-09-03 来自浙江
    #运行新增逻辑 (单独一个cell,可循环使用) bool_test = Test_rep(pro_id = 1000,pro_num = 12,cart_temp = cart,rep_temp= rep_products,pro_tem=products) if bool_test: add_pro(pro_id = 1000,pro_num = 12,cart_temp = cart,rep_temp= rep_products,pro_tem=products) show_cart(cart) #运行减少逻辑 (单独一个cell,可循环使用) bool_test = Test_rep(pro_id = 1000,pro_num = -2,cart_temp = cart,rep_temp= rep_products,pro_tem=products) if bool_test: add_pro(pro_id = 1000,pro_num = -2,cart_temp = cart,rep_temp= rep_products,pro_tem=products) show_cart(cart)
    
    
  • acondess
    2023-09-03 来自浙江
    # 判断库存是否存在商品、判断是否有库存 def Test_rep(pro_id:int,pro_num:int,cart_temp:dir,rep_temp:list,pro_tem:list): # 判断库存是否存在商品、判断是否有库存 for product in rep_temp: if pro_id == product[0]: for pro_i in pro_tem: if pro_i[0] == pro_id: print(f"库存中存在{pro_i[1]}") if product[1] >= pro_num: print(f"库存中足够") return True else: print(f"库存中存量不够") return False return False # 购物车添加商品 参数:添加的id、数量、购物车、仓库 def add_pro(pro_id:int,pro_num:int,cart_temp:dir,rep_temp:list,pro_tem:list): print(cart_temp) # 减少商品 (在add_pro传参设置pro_num为负数,理解为减少商品,但是需要判断购物车中数量足够) if cart_temp!={} and pro_num < 0 and -pro_num > cart_temp[pro_id]: print("购物车中数量不足,无法减少对应数量") else: if pro_id in cart_temp: cart_temp[pro_id] += pro_num else: cart_temp[pro_id] = pro_num print(cart_temp) # 仓库减少对应数量商品 for product in rep_temp: if pro_id == product[0]: for pro_i in pro_tem: if pro_i[0] == pro_id: product[1] -= pro_num print(f"库存中{pro_i[1]}的数量变为:{product[1]}") # 展示购物车商品及金额总计 def show_cart(cart_temp:dir): totle_price=0 print(cart_temp) for product in cart_temp: totle_price += idtoprice(product) * cart_temp[product] print(f"购买的商品是:{idtoname(product)}\t 购买的单价是:{idtoprice(product)} \t 购买的数量是{cart_temp[product]}") print(f"总价为{totle_price}")
    展开
    
    
  • acondess
    2023-09-03 来自浙江
    # 定义商品(列表) products = [[1000,"iphone","phone",12000], [1001,"ipad","pad",15000], [1002,"macbook","laptop",20000]] # 定义购物车(字典) "product_id":"prudoct_number" # cart = {1000:2,1001:1} # 定义库存(列表) [id,库存数] rep_products = [[1000,10], [1001,20], [1002,5]] # 定义购物车(字典) "product_id":"prudoct_number" cart = {} # 传入id导出名字函数方法 def idtoname(product_id): for product in products: if product[0] == product_id: return product[1] break # 传入id导出单价函数方法 def idtoprice(product_id): for product in products: if product[0] == product_id: return product[3] break
    
    
  • Geek_Mike
    2023-07-19 来自云南
    products = [[1001, 'iPhone', 'phone', 10000], [1002, 'iPad', 'pad', 12000], [1003, 'MacBook', 'laptop', 15000]] cart = {1001: 10, 1002: 10, 1003: 10} inventory = {1001:90, 1002:90, 1003:90} # 初始化商品库存 def idtoname(P_id): for i in products: if i[0] == P_id: return i[1] def idtoprice(P_id): for i in products: if i[0] == P_id: return i[3] def showcart(): TotalMoney = 0 print('当前购物车中的商品是:') for i in cart: print(f'购买商品 {idtoname(i)} 的数量是 {cart[i]} 单价是 {idtoprice(i)}') TotalMoney += idtoprice(i) * cart[i] print(f'总费用是 {TotalMoney}') def showinventory(): print('当前库存:') for i in inventory: print(f'商品 {idtoname(i)} 的库存是 {inventory[i]}') def addProduct(P_id, P_number): if P_id in cart.keys() and inventory[P_id] < P_number: print(f'{idtoname(P_id)} 产品库存剩余 {inventory[P_id]},库存不足无法添加到购物车') elif P_id in cart.keys() and inventory[P_id] >= P_number: cart[P_id] += P_number inventory[P_id] -= P_number # 库存相应减少 def delProduct(P_id, P_number): if P_id in cart.keys() and cart[P_id] < P_number: print(f'{idtoname(P_id)} 在购物车中只有 {cart[P_id]} 无法减少 {P_number} 个') elif P_id in cart.keys() and cart[P_id] >= P_number: cart[P_id] -= P_number inventory[P_id] += P_number # 购物车中该商品数量减少,库存相应增加 if cart[P_id] == 0: cart.pop(P_id) def cleancart(): for i in cart.keys(): inventory[i] += cart[I] # 把购物车里的商品数量归还到库存 cart.clear() showinventory() print() showcart() print() # addProduct(1003,100) # showinventory() # print() # showcart() # delProduct(1001,10) # showinventory() # print() # showcart() # cleancart() # showinventory() # print() # showcart()
    展开
    
    
  • Carl-Zhou
    2023-06-27 来自江苏
    # 商品 products = [[1000, "iphone", "phone", 12000], [1001, "ipad", "pad", 15000], [1002, "macbook", "laptop", 20000]] # 购物车 "product_id":"product_number" cart = {1000: 5, 1001: 2} def getProductIdAndNames(): """ 获取所有商品的id和name """ return {product[0]: product[1] for product in products} def showCart(): """ 展示购物车里的商品 以及总价""" print("你的购物车里有以下商品:") total_price = 0 for product_id, product_number in cart.items(): for product in products: if product_id == product[0]: total_price += product[3] * product_number print(f"商品编号:{product_id},商品名称:{product[1]},商品单价:{product[3]},商品数量:{product_number}") print(f"总费用为: {total_price}") def addProduct(product_id, product_number): """ 添加商品到购物车 """ # 判断商品是否存在 product_info = getProductIdAndNames() if product_id not in product_info.keys(): print(f"您添加的商品已下架!") return # 增加新商品,或已有商品数量 if product_id in cart: cart[product_id] += product_number else: cart[product_id] = product_number showCart() def delProduct(product_id): """减少已有商品数量""" # 判断商品是否存在 product_info = getProductIdAndNames() if product_id not in product_info.keys(): print(f"您要删除的商品不存在!") return # 减少商品数量 if product_id in cart: cart[product_id] -= 1 if cart[product_id] == 0: cart.pop(product_id) showCart()
    展开
    
    
  • GeekNEO
    2023-03-02 来自浙江
    # 接上 def clear_cart(): for product_id in cart.keys(): for stock in stocks: if stock[0] == product_id: stock[3] += cart[product_id] return "清空购物车,库存更新" cart.clear() # del_product(1001) add_product(1002, 4) print(cart) print(stocks) # print(f"产品: {id_to_name(1002)} 的价格为:{id_to_price(1002)}") show_cart()
    
    
  • GeekNEO
    2023-03-02 来自浙江
    products = [[1000, "iPhone", "Phone", 12000], [1001, "iPad", "Pad", 15000], [1002, "MacBook", "Laptop", 20000]] stocks = [[1000, "iPhone", "Phone", 50], [1001, "iPad", "Pad", 100], [1002, "MacBook", "Laptop", 120]] cart = {1000: 5, 1001: 2} def id_to_name(product_id: int) -> str: for product in products: if product[0] == product_id: return product[1] def id_to_price(product_id: int) -> int: for product in products: if product[0] == product_id: return product[3] def stock_num(product_id: int) -> str: for stock in stocks: if stock[0] == product_id: return stock[3] def stock_reduce(product_id: int, product_number: int) -> str: for stock in stocks: if stock[0] == product_id: stock[3] -= product_number return "库存更新" def stock_increase(product_id: int) -> str: for stock in stocks: if stock[0] == product_id: stock[3] += 1 return "库存更新" def show_cart(): total_money = 0 for product in cart: print(f"购买商品 {id_to_name(product)} 的数量为 {cart[product]}") total_money += id_to_price(product) * cart[product] print(f"总费用:{total_money}") def add_product(product_id, product_number): # 增加商品或已有商品 if product_number <= stock_num(product_id): if product_id in cart.keys(): cart[product_id] += product_number else: cart[product_id] = product_number # 如果购物车种新增,那么库存就需要减少 stock_reduce(product_id, product_number) print(f"商品 {id_to_name(product_id)} 成功添加进购物车 {product_number} 个!") else: print(f"商品 {id_to_name(product_id)} 库存不足,无法添加!") def del_product(product_id): if product_id in cart.keys(): cart[product_id] -= 1 stock_increase(product_id) if cart[product_id] == 0: cart.pop(product_id)
    展开
    
    
  • 朱雯
    2023-02-15 来自美国
    products = [[1000, 'iphone', 'phone', 12000], [1001, 'ipad', 'pad', 15000],] cart = {1000: 5, 1001: 2} kucun = {1000: 10, 1001: 20, 1002: 5} def idtoprice(proid): for product in products: if product[0] == proid: return product[3] def idtoname(proid): for product in products: if product[0] == proid: return product[1] def showCart(): total = 0 for product in cart: print(f"购买商品有{idtoname(product)},数量为{cart[product]}") total += cart[product] * idtoprice(product) print(f"商品的总价为{total}") def showKucun(): for product in kucun: print(f"库存商品有{idtoname(product)},数量为{kucun[product]}") def selCart(proid, pronum): if proid in cart.keys(): if cart[proid] >= pronum: return True return False def selKucun(proid, pronum): if proid in kucun.keys(): if kucun[proid] >= pronum: return True return False def addCart(proid, pronum): if proid in cart.keys(): cart[proid] += pronum else: cart[proid] = pronum def addKucun(proid, pronum): if proid in kucun.keys(): kucun[proid] += pronum else: kucun[proid] = pronum def delKucun(proid, pronum): if selKucun(proid, pronum): kucun[proid] -= pronum return True else: return False def delCart(proid, pronum): if selCart(proid, pronum): cart[proid] -= pronum return True else: return False def clearCart(proid): if proid in cart.keys(): if cart[proid] == 0: del cart[proid] def clearKucun(proid): if proid in kucun.keys(): if kucun[proid] == 0: del kucun[proid] def addCarts(proid, pronum): if not delKucun(proid, pronum): return addCart(proid, pronum) clearKucun(proid) def delCarts(proid, pronum): if not delCart(proid, pronum): return addKucun(proid, pronum) clearCart(proid)
    展开
    
    