Python实现有用户和商家入口的购物商城
下面小编就为大家带来一篇python 实现购物商城,含有用户入口和商家入口的示例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这是模拟淘宝的一个简易的购物商城程序。
用户入口具有以下功能:
登录认证
可以锁定用户
立即学习“Python免费学习笔记(深入)”;
密码输入次数大于3次,锁定用户名
连续三次输错用户名退出程序
可以选择直接购买,也可以选择加入购物车
用户使用支付密码完成支付,支付密码连续输入错误达3次,锁定用户名
商家入口具有以下功能:
登录认证
可以锁定用户
立即学习“Python免费学习笔记(深入)”;
密码输入次数大于3次,锁定用户名
连续三次输错用户名退出程序
商家可以编辑商品
上架新品
下架商品
修改商品信息:商品名、单价、库存
每个用户的用户名、密码、余额、支付密码,以行记录定义在 user_list.txt 文件中,以逗号分隔;
每件商品的商品名、单价、库存,以行记录定义在 product_list.txt 文件中,以逗号加一个空格分隔;
被锁定用户名记录在 lock_list.txt 文件中,以行分隔;
商家的用户名、密码定义在 seller_list.txt 文件中,以逗号分隔;
# Joe Youngimport getpassimport os# 调用os模块的system方法传入'cls'参数,清屏os.system('cls') while True: entrance = input('请选择:1. 用户登陆2. 商家登陆>>>') if entrance != '1' and entrance != '2': print('输入有误,请重试...') else: break# 打印商品列表def print_product_list(): index = 1 with open('product_list.txt', 'r') as product_file: for product_line in product_file: L = [commodity, price, stock] = product_line.strip('').split(', ') commodity_list.append(L) print((str(index) + '. ' + commodity).ljust(20) + ('单价:' + price + '元').ljust(15) + '库存:' + stock) index += 1 return# 用户入口if entrance == '1': info = [] # 存放用户的信息,初始为空 if_payed = True # if_payed 表示订单是否已支付 username = '' # 登录接口 count = 0 while count len(commodity_list): print('此商品不存在,请重试...') continue item_name = commodity_list[i-1][0] # 商品名称 item_price = commodity_list[i-1][1] # 商品价格 item_stock = commodity_list[i-1][2] # 商品库存 print('您已选择了 %s ,请输入购买的数量,或输入 b 重新选择:' % item_name) back = False while True: num = input('>>>') if num == 'b': back = True break if not num.isdigit(): print('输入格式有误,请重试...') continue if int(num) > int(item_stock): print('数量大于库存,请重试...') continue if int(num) == 0: print('数量应大于0,请重试...') break if back: continue item = [item_name, item_price, num] print('您已选择了 %s,单价:%s 元,数量:%s,您想立即购买还是加入购物车?' % (item_name, item_price, num)) print(' 1. 立即购买2. 加入购物车') while True: choice = input('>>>') if not (choice == '1' or choice == '2'): print('输入有误,请重试...') continue break user_balance = int(info[2]) # 立即购买 if choice == '1': amount = int(item_price) * int(num) count = 0 cancel = False while count = amount: user_balance -= amount print('支付成功!您已成功购买 %s ,单价:%s 元,数量:%s,总金额:%s 元,账户余额:%s 元' % (item_name, item_price, num, amount, user_balance)) lines = open('product_list.txt', 'r').readlines() # 定位到用户所购买的商品所在行,分割成列表赋值给select select = lines[i-1].strip('').split(', ') # 修改商品的库存 select[-1] = (str(int(select[-1]) - int(num)) + '') # 拼接成字符串 lines[i-1] = ', '.join(select) # 将修改写回文件 open('product_list.txt', 'w').writelines(lines) lines = open('user_list.txt', 'r').readlines() # 修改用户余额 for line in lines: if username in line.split(','): # 定位到用户名所在行 j = lines.index(line) # 获取用户名所在行的行号索引 select = line.split(',') # 分割用户名所在行赋值给列表select select[-2] = str(user_balance) # 修改用户余额 lines[j] = ','.join(select) # 修改后的列表拼接成字符串,覆盖用户名所在行 open('user_list.txt', 'w').writelines(lines) # 将修改写回文件 else: print('对不起,您的余额不足...') else: # 加入购物车 j = 0 for j in range(len(shopping_cart)): # 如果商品在购物车里面,更新商品数量 if item_name in shopping_cart[j]: shopping_cart[j][2] = str(int(shopping_cart[j][2]) + int(num)) break # 商品若不在购物车,则添加到购物车 else: shopping_cart.append(item) print('成功加入购物车!') while True: choice = input('是否继续购买?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('输入格式有误,请重试...') continue break if choice == 'Y' or choice == 'y': continue else: break # 如果购物车不为空 if shopping_cart: print('您的购物车里有以下宝贝:') i = 1 total_sum = 0 for item in shopping_cart: (commodity, price, number) = (item[0], item[1], item[2]) print((str(i) + '. ' + commodity).ljust(20) + ('单价:' + price + ' 元').ljust(15) + '数量:' + number) total_sum += int(price) * int(number) i += 1 print('合计:%d 元' % total_sum) while True: if_buy = input('是否结算?(Y/N):') if not (if_buy == 'Y' or if_buy == 'y' or if_buy == 'N' or if_buy == 'n'): print('输入有误,请重试...') continue break while True: # 结算 if if_buy == 'Y' or if_buy == 'y': count = 0 cancel = False while count >>') if not (choice == '1' or choice == '2' or choice == '3' or choice == 'q'): print('输入有误,请重试...') continue # 上架新品 if choice == '1': while True: if_add = False # 是否添加商品的标志,初始为False new_commodity = input('输入商品名:') product_file = open('product_list.txt', 'r') for product_line in product_file: commodity = product_line.strip('').split(', ')[0] # 获取商品列表中的商品名 if new_commodity == commodity: print('此商品已在货架上...') continue else: while True: if_sure = input('确定上架新品 %s 吗?(Y/N):' % new_commodity) if not (if_sure == 'Y' or if_sure == 'y' or if_sure == 'N' or if_sure == 'n'): print('输入有误,请重试...') continue break # 确定上架新品 if if_sure == 'Y' or if_sure == 'y': while True: # 输入单价 price = input('请输入单价:') if not price.isdigit(): print('输入有误,请重试...') continue break while True: # 输入库存 stock = input('请输入库存:') if not stock.isdigit(): print('输入有误,请重试...') continue break new_line = '' + new_commodity + ', ' + price + ', ' + stock open('product_list.txt', 'a').writelines(new_line) print('成功上架新品 %s ,单价 %s 元,库存 %s 件' % (new_commodity, price, stock)) while True: option = input('是否继续添加?(Y/N):') if not (option == 'Y' or option or option == 'N' or option == 'n'): print('输入有误,请重试...') continue break if option == 'Y' or option == 'y': if_add = True break # 跳出for循环 else: break # 取消上架新品 else: if_add = False break # 跳出for循环 product_file.close() if if_add is True: continue else: break # 跳出while循环 # 下架商品 elif choice == '2': while True: del_num = input('请输入您想下架商品的序号:') if not (del_num.isdigit() or int(del_num) > 0 and int(del_num) 0 and int(i) >>") if not (j == 'c' or j == '1' or j == '2' or j == '3'): print("输入有误,请重试...") continue break if j == 'c': break else: mod_commodity_info(i, j) else: exit('您已退出商城...') else: exit('您已退出商城...')