def shopping(): #商品列表保存在shap_data中的商品文件中。 product_list = shop_interface.get_product_list_interface() print(product_list) while True: for index, name_price in enumerate(product_list): name, price = name_price print(f'商品编号: {index}', f'商品名称:{name}', f'商品价格:{price}') choice = input('请选择商品编号(是否结账请输入Y or N):') if choice.isdigit(): if int(choice) in range(len(product_list)): choice = int(choice) name = product_list[choice][0] price = float(product_list[choice][1]) flag, msg = shop_interface.add_shop_car_interface( login_user, name, price) if flag: print(msg) elif choice == 'Y' or choice == 'y': flag, msg = shop_interface.pay_shop_car_interface(login_user) if flag: print(msg) break else: print(msg)
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(): 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(): goods_list = [['凤爪', 50], ['T-shirt', 150], ['macbook', 5000], ['iphoneX', 3000]] #获取用户的余额 user_balance = bank_interface.check_blance_interface(user_info['user']) #购物车 shop_car = {} #初始总额 count = 0 while True: #打印出所有商品 for num, goods in enumerate(goods_list, 1): print(num, goods) print('----输入q退出购买') choice = input('请输入商品编号(结账输入q):').strip() #判断输入的是符合的数字,只是选购功能,都放在购物车里面,没有结算 if choice.isdigit() and 4 >= int(choice) >= 1: num = input('请输入你需要购买的数量:') # 获取商品名称和单价 goods, price = goods_list[int(choice) - 1] #判断用户余额是否大于商品单价 if user_balance >= price: #花费 money = price * int(num) #判断用户余额是否大于总花费 if user_balance >= money: #用户消费 if goods not in shop_car: shop_car[goods] = num else: shop_car[goods] += num count += money else: print('余额买不了这么多东西') else: print('余额不足单价') #判断是否需要结账 elif choice == 'q': commint = input('是否确认结账,请输入y/n:') if commint == 'y': #调用商城购物车支付功能,并调用银行支付接口 flag, msg = shop_interface.buy_shop_interface( user_info['user'], count) if flag: print(msg) break else: print(msg) #如果不支付就把商品全部加到用户的购物车信息里面 elif commint == 'n': flag, msg = shop_interface.add_shop_car_interface( user_info['user'], shop_car) if flag: print(msg) break else: print('请输入正确的数字')
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)