def shopping(): #商品列表 shop_list = [ ['戏志才',800], ['关索',600], ['关银屏',760] ] # 初始化当前购物车 shopping_car = {} while True: for index,shop in enumerate(shop_list): shop_name,shop_price = shop print(f'商品编号[{index}]', f'商品名称[@@{shop_name}]', f''f'商品单价[{shop_price}$]') #让用户选择 choice = input('请输入目标商品编号(结算输入T,加入购物车请输入F):>>>').strip() if choice =='T': #调用支付接口 result,msg = shop_interface.shopping_interface(login_stat,shopping_car) print(result) if result == True: print(msg) elif result == False: print(msg) else: print('未知错误') continue elif choice =='F': if not shopping_car: print('购物车是空的,不能添加') continue result,msg = shop_interface.add_shop_car_interface( login_stat,shopping_car ) if result: print(msg) break if not choice.isdigit(): print('商品编号仅能输入数字哦') continue choice = int(choice) if choice not in range(len(shop_list)): print('没有这个商品编码,请确认后重新输入') continue #获取商品列表与单价 shop_name,shop_price = shop_list[choice] #5加入购物车 if shop_name in shopping_car: shopping_car[shop_name][1] +=1 else: shopping_car[shop_name] = [shop_price,1] print(shopping_car)
def shopping(): shop_list=[ ['电脑',5998], ['衣服',877], ['零食',556], ['机票',887], ] shopping_car={} while True: for index,shop in enumerate(shop_list): shop_name,price=shop print(f'商品名称:{shop_name}', f'商品编号:{index}', f'商品价格:{price}' ) chioce=input('请输入商品编号or结账y/n/q:').strip() if chioce == 'q': break if chioce =='y': if not shopping_car: print('购物车是空的,不能结算') continue flag,msg=shop_interface.shopping_interface(login_user,shopping_car) if flag: print(msg) continue else: print(msg) elif chioce=='n': if not shopping_car: print('购物车是空的,请重新输入') continue flag,msg=shop_interface.add_shop_car_interface(login_user,shopping_car) if flag: print(msg) continue else: print(msg) if not chioce.isdigit(): print('请输入数字') continue chioce=int(chioce) if chioce not in range(len(shop_list)): print('请输入正确的编号') continue shop_name,shop_price=shop_list[chioce] if shop_name in shopping_car: shopping_car[shop_name][1]+=1 else: shopping_car[shop_name]=[shop_price,1] print(shopping_car)
def shopping(): shop_list = [ ['oppo v480', 3899], ['vivo X200', 3999], ['iphone 123', 99999], ['xiaomi 100', 10000], ] shopping_car = {} # 初始化购物车,格式为{'商品名称':['单价','数量']} while True: for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'商品编号为:{index}', f'商品名称:{shop_name}', f'商品价格:{shop_price}') choice = input('请输入商品编号:(输入y or n选择是否结账)').strip() # 输入y进入支付 if choice == 'y': if not shopping_car: print('购物车是空的,请选择至少一个商品进行支付!') continue flag, msg = shop_interface.shopping_interface( login_user, shopping_car) if flag: print(msg) break else: print(msg) # 输入n添加商品到购物车 elif choice == 'n': if not shopping_car: print('请至少选择一个商品!') continue flag, msg = shop_interface.add_shop_car_interface( login_user, shopping_car) if flag: print(msg) break if not choice.isdigit(): print('请输入正确的商品编号!') continue choice = int(choice) if choice not in range(len(shop_list)): print('未找到您搜索的商品的编号,请重新输入!') continue shop_name, shop_price = shop_list[choice] if shop_name in shopping_car: shopping_car[shop_name][1] += 1 else: shopping_car[shop_name] = [shop_price, 1] print('当前购物车:', shopping_car)
def shopping(): ''' 思路 1.商品信息存在于文件当中,显示商品信息时需要从文件中读取,调用shop_interface中的接口获取商品信息 2.用户当前有个购物车,存储当前选择的商品,若支付成功不用加入购物车,若没有支付,加进用户的购物车 购物车中没有的商品,进行添加,商品数量为1 购物车中有的商品,商品数量加1 ''' shop_dic = shop_interface.get_shop_info() # print(shop_dic) print('精品商店'.center(20, '=')) for index, shop in shop_dic.items(): shop_name, price = shop print(' {}.{}:{}$'.format(index, shop_name, price)) print('24h营业'.center(22, '=')) shop_car = {} # {'商品名字':[价格,数量]} while True: # 接手用户选择的商品 choice = input('请输入商品的编号(y(支付) or n(不支付)):').strip() if choice == 'y': # 检测用户当前购物车中有无商品 if shop_car == {}: print('当前没有选择商品,请选择:') continue # 调用购物接口 flag, msg = shop_interface.shopping_interface(login_user, shop_car) print(msg) if flag: break elif choice == 'n': flag, msg = shop_interface.add_shop_interface(login_user, shop_car) print(msg) if flag: break if not choice in shop_dic: print('请输入正确的商品编号:') continue # 购物车更新 shop_name, shop_price = shop_dic[choice] if shop_name in shop_car: shop_car[shop_name][1] += 1 else: shop_car[shop_name] = [shop_price, 1] print('购物车:', shop_car)
def shopping(): # shop_list = { # '0': {'name': 'haha', 'price': 30}, # } shopping_car = {} shop_list = [ ['hahah', 30], ['hehe', 100], ['iphone', 200], ['oneplus', 1000], ['xiaomi', 500], ['huawei', 800], ['sanxing', 999], ] while True: for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'{index}:{shop_name} 价格:{shop_price}') choice = input('请输入购买商品编号 (y结算,n添加购物车):').strip() if choice == 'y': if not shopping_car: print('购物车是空的') continue flag, msg = shop_interface.shopping_interface() if flag: print(msg) break else: print(msg) elif choice == 'n': flag, msg = shop_interface.add_shopcar_interface(login_user,shopping_car) if flag: print(msg) break if not choice.isdigit(): continue choice = int(choice) if choice not in range(len(shop_list)): print('商品不存在') continue shop_name, shop_price = shop_list[choice] if shop_name in shopping_car: shopping_car[shop_name][1] += 1 else: shopping_car[shop_name] = [shop_price, 1]
def check_shop_car(): shop_car = shop_interface.check_shopping_car(login_stat) print(shop_car) while True: choice = input('[干啥嘞:支付:p/整理购物车:c/清空购物车:clear]>>>>>') if choice not in ('p','c','clear'): print('您的输入有误,请重新输入') if choice == 'p': if not shop_car: print('您的购物车是空的哦,快去添加商品吧....') continue result,msg = shop_interface.shopping_interface(login_stat,shop_car) if result == True: print ('success',msg) elif result == False: print('fail',msg) else: print('未知错误') elif choice == 'c': choice = input('修改商品数量:1/删除商品:2') if choice == '1': result,msg = shop_interface.change_shop_car(login_stat) if result == True: print ('修改成功',msg) elif result == False: print('未做修改',msg) else: print('未知错误') elif choice == '2': result,msg = shop_interface.del_shop(login_stat) if result == True: print ('修改成功',msg) elif result == False: print('未做修改',msg) else: print('未知错误') elif choice == 'clear': result, msg = shop_interface.clear_shop(login_stat) if result == True: print('修改成功', msg) elif result == False: print('未做修改', msg) else: print('未知错误')
def shopping(): shop_list = [ ['灌汤包', 30], ['鸡米花', 5], ['鸭腿饭', 15], ['脆皮鸡米饭', 10], ] shopping_car = {} # {商品名称:【商品单价, 数量】} while True: # 枚举:enumerate(可迭代对象) ---> (索引,值) print("=========请选择你的商品=========") for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'商品编号为:[{index}]', f'商品名称:[{shop_name}]', f'商品单价:[{shop_price}]') print("=========24h自助服务=========") choice = input('请输入商品编号(是否结账y/n):').strip() if choice == 'y': if not shopping_car: print('购物车为空,不能支付,请重新输入') continue flag, msg = shop_interface.shopping_interface( login_user, shopping_car ) if flag: print(msg) break else: print(msg) elif choice == 'n': if not shopping_car: print('购物车为空,不能添加,请重新输入') continue flag, msg = shop_interface.add_shop_car_interface( login_user, shopping_car ) if flag: print(msg) break if not choice.isdigit(): print('请输入正确的编号') continue choice = int(choice) if choice not in range(len(shop_list)): print('请输入正确的编号') continue shop_name, shop_price = shop_list[choice] if shop_name in shopping_car: shopping_car[shop_name][1] += 1 else: shopping_car[shop_name] = [shop_price, 1] print('当前购物车:', shopping_car)
def shopping(): # 从文件中读取商品信息 # 不从文件中读取商品信息,直接写入 shop_list = [['麻辣牛肉', 30], ['衣服', 500], ['手机', 3000], ['自行车', 9000], ['Macbook', 12000]] # 初始化购物车 shopping_car = {} while True: #打印商品信息 for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'商品编号:[{index}]', f'商品名称:[{shop_name}]', f'商品单价:[{shop_price}]元') # 让用户根据商品编号选择商品 choice = input('请输入商品编号(输入y结账或者输入n添加购物车): ').strip() if choice == 'y': if not shopping_car: print('购物车为空,不能结账.请重新输入.') continue flag, msg = shop_interface.shopping_interface( login_user, shopping_car) if flag: print(msg) break else: print(msg) elif choice == 'n': if not shopping_car: print('购物车为空,不能添加到数据库.请重新输入.') continue flag, msg = shop_interface.add_shop_car_interface( login_user, shopping_car) if flag: print(msg) break # 判断用户是否输入数字 if not choice.isdigit(): print('请输入正确的编号!') continue choice = int(choice) # len(shop_list) == 5 range(5) = 4,3,2,1,0 if choice not in range(len(shop_list)): print('请输入正确的编号!') continue # 拿到商品名称和商品单价 shop_name, shop_price = shop_list[choice] # 加入购车之前先在接口层拿到购物车 # 判断用户的购车车是否有数据,有则数据+1 没有则新填一条 if shop_name in shopping_car: shopping_car[shop_name][1] += 1 pass else: shopping_car[shop_name] = [shop_price, 1] print('当前购物车: ', shopping_car)
def shopping(): # 这里使用枚举类型,仅做演示,不建议使用 shop_list = [ ['上海灌汤包', 30], # 0 ['海底捞', 399], # 1 ['广东凤爪', 28], ['香港地道鱼丸', 9999], ] # 初始化购物车 shopping_car = {} # {'商品名称': ['单价', '数量']]} while True: print('==================== 购物商城 ====================') for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'商品编号为:[{index}]', f'商品名称:[{shop_name}]', f'商品单价:[{shop_price}]') print('==================== END ====================') # 接收用户输入的指令 choice = input('请输入商品编号(是否结账输入y or n): ').strip() # 当指令为y时 if choice == 'y': if not shopping_car: print("购物车为为空,无需支付,请重新输入") continue # 调用支付接口 flag, msg = shop_interface.shopping_interface( username=login_user, shopping_car=shopping_car) if flag: print(msg) break else: print(msg) # 将商品添加到购物车并退出 break if choice == 'n': if not shopping_car: print("购物车为空,无需加入购物车,请先添加到购物车!!!") continue # 调用添加购物车功能 falg, msg = shop_interface.add_shop_car_interface( username=login_user, shopping_car=shopping_car) if falg: print(msg) break if not choice.isdigit(): print('请输入正确的编号') continue choice = int(choice) if choice not in range(len(shop_list)): print("请输入正确的编号") continue # 获取选择商品名称和价格 shop_name, shop_price = shop_list[choice] # 添加商品到购物车 if shop_name not in shopping_car: # 判断商品是否存在购物车,不存在则添加购物车 {'商品名称': ['单价', '数量']]} shopping_car[shop_name] = [shop_price, 1] else: # 当购物车存在此类商品则商品数加一 shopping_car[shop_name][1] += 1 print('当前购物车: ', shopping_car)
def shopping(): # 不从文件中读取商品数据,直接写(ps: 课后作业,从文件中读取商品数据) # 1)创建一个商品列表 # shop_list = { # '0': {'name': '上海灌汤包', 'price': 30}, # } # 列表套列表的商品数据 # [[商品名称1, 商品单价1], [商品名称2, 商品单价2]...] shop_list = [ ['上海灌汤包', 30], # 0 ['矮跟写真抱枕', 250], # 1 ['广东凤爪', 28], ['香港地道鱼丸', 15], ['坦克', 100000], ['macbook', 20000], ] # 初始化当前购物车: shopping_car = {} # {'商品名称': ['单价', '数量']]} while True: # 1) 打印商品信息,让用户选择 # 枚举: enumerate(可迭代对象) ---> (可迭代对象的索引, 索引对应的值) # 枚举: enumerate(可迭代对象) ---> (0, ['上海灌汤包', 30]) print('============欢迎来到有趣用品商城============') for index, shop in enumerate(shop_list): shop_name, shop_price = shop print(f'商品编号为:[{index}]', f'商品名称:[{shop_name}]', f'商品单价:[{shop_price}]') print('================24小时服务哦==============') # 2) 让用户根据商品编号进行选择 choice = input('请输入商品编号(是否结账输入y or n): ').strip() # 2.1) 输入的是 y 进入支付结算功能 if choice == 'y': if not shopping_car: print('购物车是空的,不能支付,请重新输入!') continue # 6)调用支付接口进行支付 flag, msg = shop_interface.shopping_interface( login_user, shopping_car) if flag: print(msg) break else: print(msg) # 2.2) 输入的是 n 添加购物车 elif choice == 'n': # 判断当前用户是否添加过购物车 if not shopping_car: print('购物车是空的,不能添加,请重新输入!') continue # 7)调用添加购物车接口 flag, msg = shop_interface.add_shop_car_interface( login_user, shopping_car ) if flag: print(msg) break if not choice.isdigit(): print('请输入正确的编号!') continue choice = int(choice) # 3) 判断choice是否存在 if choice not in range(len(shop_list)): print('请输入正确的编号!') continue # 4) 获取商品名称和与单价 shop_name, shop_price = shop_list[choice] # 5)加入购物车 # 5.1) 判断用户选择的商品是否重复,重复则数量 +1 if shop_name in shopping_car: # [shop_price, 1][1] ---> 1 += 1 # 添加商品数量 shopping_car[shop_name][1] += 1 else: # 否则数量默认为1 # {'商品名称': ['单价', '数量']]} shopping_car[shop_name] = [shop_price, 1] print('当前购物车: ', shopping_car)