Python implements a shopping mall with user and merchant entrances

巴扎黑
Release: 2017-09-16 10:04:53
Original
1691 people have browsed it

The following editor will bring you an example of implementing a shopping mall in Python, including user entrance and merchant entrance. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

This is a simple shopping mall program that simulates Taobao.

The user entrance has the following functions:

Login authentication

Can lock the user

number of password inputs More than 3 times, the username will be locked

Enter the wrong username three times in a row to exit the program

You can choose to purchase directly or add to the shopping cart

Users use the payment password to complete the payment , the payment password has been entered incorrectly for 3 consecutive times, and the user name is locked

The merchant entrance has the following functions:

Login authentication

You can lock the user

The number of times the password is entered is more than 3 times, the username is locked

Enter the wrong username three times in a row to exit the program

The merchant can edit the product

New products on the shelves

Products off the shelves

Modify product information: product name, unit price, inventory

The username, password, balance, and payment password of each user are recorded in rows Defined in the user_list.txt file, separated by commas;

The product name, unit price, and inventory of each product are defined in the product_list.txt file as line records, separated by commas and a space;

The locked username is recorded in the lock_list.txt file, separated by lines;

The merchant's username and password are defined in the seller_list.txt file, separated by commas;


# Joe Young

import getpass
import os

# 调用os模块的system方法传入'cls'参数,清屏
os.system('cls') 

while True:
 entrance = input('请选择:\n\t1. 用户登陆\n\t2. 商家登陆\n>>>')
 if entrance != '1' and entrance != '2':
  print('\n输入有误,请重试...\n')
 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('\n').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 < 3:
  username = input(&#39;\n用户名: &#39;)

  # 打开锁定列表文件
  with open(&#39;lock_list.txt&#39;, &#39;r+&#39;) as lock_file: 
   for lock_line in lock_file:
    # 用户名在锁定名单里面,则退出程序
    if username == lock_line.strip(&#39;\n&#39;): 
     exit(&#39;\n用户名 %s 已被锁定,请联系管理员...&#39; % username)

  login = False # 登录标志,初始为False

  # 打开用户名列表文件,读权限
  user_file = open(&#39;user_list.txt&#39;, &#39;r&#39;)  

  for user_line in user_file:
   # 获取每行的用户信息,用户名、密码、余额、支付密码,存入info列表
   info = [user, passwd, balance, pay_passwd] = user_line.strip(&#39;\n&#39;).split(&#39;,&#39;)
   # 用户名匹配,则进入密码输入环节
   if user == username: 
    n = 0
    # 3次输入机会
    while n < 3: 
     password = getpass.getpass(&#39;密码: &#39;)
     # 密码匹配,显示登录成功
     if passwd == password: 
      print(&#39;\n欢迎 %s 登录商城,祝您购物愉快!\n&#39; % username)
      login = True  # 登录标志赋值为True
      break
     # 密码不匹配
     else: 
      # n = 2 时是最后一次机会,不必提示还剩下0次机会
      if n != 2: 
       print(&#39;\n密码错误,请重新输入,您还有 %d 次机会\n&#39; % (2-n))
     n += 1
    # 密码错误次数达到3次,锁定用户名,退出程序
    else: 
     open(&#39;lock_list.txt&#39;, &#39;w&#39;).write(username + &#39;\n&#39;)
     exit(&#39;\n错误次数过多,账户已被锁定...&#39;)

    # 登录成功,跳出for循环
    if login: 
     break
  else:
   if count != 2:
    print(&#39;\n用户名不存在,请重试,您还有 %d 次机会&#39; % (2-count))

  user_file.close()

  count += 1

  # 登录成功,跳出while循环
  if login: 
   break

 else:
  exit(&#39;\n错误次数过多,程序已退出...&#39;)

 # 购买程序
 shopping_cart = [] # 购物车初始为空
 commodity_list = []

 print_product_list()

 while True:
  i = input(&#39;\n请选择商品(输入序号),或输入 c 取消购买:&#39;)

  if i == &#39;c&#39;:
   while True:
    a = input(&#39;\n是否继续购买?(Y/N):&#39;)
    if a == &#39;n&#39; or a == &#39;N&#39;:
     exit(&#39;\n交易结束...&#39;)
    elif a == &#39;y&#39; or a == &#39;Y&#39;:
     break
    else:
     print(&#39;\n输入格式有误,请重试...&#39;)
     continue

  if not i.isdigit():
   print(&#39;\n输入格式有误,请重试...&#39;)
   continue

  i = int(i)

  if i <= 0 or i > len(commodity_list):
   print(&#39;\n此商品不存在,请重试...&#39;)
   continue

  item_name = commodity_list[i-1][0]  # 商品名称
  item_price = commodity_list[i-1][1]  # 商品价格
  item_stock = commodity_list[i-1][2]  # 商品库存

  print(&#39;\n您已选择了 %s ,请输入购买的数量,或输入 b 重新选择:&#39; % item_name)

  back = False

  while True:
   num = input(&#39;>>>&#39;)
   if num == &#39;b&#39;:
    back = True
    break
   if not num.isdigit():
    print(&#39;输入格式有误,请重试...&#39;)
    continue
   if int(num) > int(item_stock):
    print(&#39;数量大于库存,请重试...&#39;)
    continue
   if int(num) == 0:
    print(&#39;数量应大于0,请重试...&#39;)
   break
  if back:
   continue

  item = [item_name, item_price, num]

  print(&#39;\n您已选择了 %s,单价:%s 元,数量:%s,您想立即购买还是加入购物车?\n&#39; % (item_name, item_price, num))
  print(&#39;\t1. 立即购买\n\t2. 加入购物车\n&#39;)

  while True:
   choice = input(&#39;>>>&#39;)
   if not (choice == &#39;1&#39; or choice == &#39;2&#39;):
    print(&#39;输入有误,请重试...&#39;)
    continue
   break

  user_balance = int(info[2])

  # 立即购买
  if choice == &#39;1&#39;: 
   amount = int(item_price) * int(num)
   count = 0
   cancel = False

   while count < 3:
    user_pay_passwd = getpass.getpass(&#39;\n请输入支付密码,或输入 c 放弃支付:&#39;)
    if user_pay_passwd == &#39;c&#39;:
     print(&#39;\n取消支付成功...&#39;)
     cancel = True
     break
    elif user_pay_passwd != info[3]:
     if count != 2:
      print(&#39;\n密码错误,请重试,您还有 %d 次机会...&#39; % (2-count))
     count += 1
    else:
     break

   if count == 3:
    with open(&#39;lock_list.txt&#39;, &#39;w&#39;) as lock_file:
     lock_file.write(username + &#39;\n&#39;)
    exit(&#39;密码错误,账户已被锁定...&#39;)

   if cancel:
    while True:
     choice = input(&#39;\n是否继续购买?(Y/N):&#39;)
     if not (choice == &#39;Y&#39; or choice == &#39;y&#39; or choice == &#39;N&#39; or choice == &#39;n&#39;):
      print(&#39;\n输入格式有误,请重试...&#39;)
      continue
     break
    if choice == &#39;Y&#39; or choice == &#39;y&#39;:
     continue
    else:
     break

   # 如果用户的账户余额大于总金额
   if user_balance >= amount: 
    user_balance -= amount
    print(&#39;\n支付成功!您已成功购买 %s ,单价:%s 元,数量:%s,总金额:%s 元,账户余额:%s 元&#39;
      % (item_name, item_price, num, amount, user_balance))
    lines = open(&#39;product_list.txt&#39;, &#39;r&#39;).readlines()
    # 定位到用户所购买的商品所在行,分割成列表赋值给select
    select = lines[i-1].strip(&#39;\n&#39;).split(&#39;, &#39;)    
    # 修改商品的库存
    select[-1] = (str(int(select[-1]) - int(num)) + &#39;\n&#39;) 
    # 拼接成字符串
    lines[i-1] = &#39;, &#39;.join(select)       
    # 将修改写回文件
    open(&#39;product_list.txt&#39;, &#39;w&#39;).writelines(lines)   

    lines = open(&#39;user_list.txt&#39;, &#39;r&#39;).readlines()
    # 修改用户余额
    for line in lines: 
     if username in line.split(&#39;,&#39;):  # 定位到用户名所在行
      j = lines.index(line)   # 获取用户名所在行的行号索引
      select = line.split(&#39;,&#39;)  # 分割用户名所在行赋值给列表select
      select[-2] = str(user_balance) # 修改用户余额
      lines[j] = &#39;,&#39;.join(select)  # 修改后的列表拼接成字符串,覆盖用户名所在行
      open(&#39;user_list.txt&#39;, &#39;w&#39;).writelines(lines) # 将修改写回文件
   else:
    print(&#39;\n对不起,您的余额不足...&#39;)

  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(&#39;\n成功加入购物车!&#39;)

  while True:
   choice = input(&#39;\n是否继续购买?(Y/N):&#39;)
   if not (choice == &#39;Y&#39; or choice == &#39;y&#39; or choice == &#39;N&#39; or choice == &#39;n&#39;):
    print(&#39;\n输入格式有误,请重试...&#39;)
    continue
   break

  if choice == &#39;Y&#39; or choice == &#39;y&#39;:
   continue
  else:
   break

 # 如果购物车不为空
 if shopping_cart: 
  print(&#39;\n您的购物车里有以下宝贝:\n&#39;)
  i = 1
  total_sum = 0
  for item in shopping_cart:
   (commodity, price, number) = (item[0], item[1], item[2])
   print((str(i) + &#39;. &#39; + commodity).ljust(20) + (&#39;单价:&#39; + price + &#39; 元&#39;).ljust(15) + &#39;数量:&#39; + number)
   total_sum += int(price) * int(number)
   i += 1
  print(&#39;\n合计:%d 元&#39; % total_sum)

  while True:
   if_buy = input(&#39;\n是否结算?(Y/N):&#39;)
   if not (if_buy == &#39;Y&#39; or if_buy == &#39;y&#39; or if_buy == &#39;N&#39; or if_buy == &#39;n&#39;):
    print(&#39;\n输入有误,请重试...&#39;)
    continue
   break

  while True:
   # 结算
   if if_buy == &#39;Y&#39; or if_buy == &#39;y&#39;: 
    count = 0
    cancel = False

    while count < 3:
     user_pay_passwd = getpass.getpass(&#39;\n请输入支付密码,或输入 c 放弃支付:&#39;)
     if user_pay_passwd == &#39;c&#39;:
      print(&#39;\n取消支付成功...&#39;)
      cancel = True
      break
     elif user_pay_passwd != info[3]:
      if count != 2:
       print(&#39;\n密码错误,请重试,您还有 %d 次机会...&#39; % (2-count))
      count += 1
     else:
      break

    if cancel:
     if_payed = False

    elif count == 3:
     with open(&#39;lock_list.txt&#39;, &#39;w&#39;) as lock_file:
      lock_file.write(username + &#39;\n&#39;)
     exit(&#39;\n密码错误,账户已被锁定...&#39;)

    else:
     if total_sum <= user_balance:
      user_balance -= total_sum
      print(&#39;\n支付成功!您已成功购买以下商品:\n&#39;)
      i = 1
      for item in shopping_cart:
       (commodity, price, number) = (item[0], item[1], item[2])
       print((str(i) + &#39;. &#39; + commodity).ljust(20) +
         (&#39;单价:&#39; + price + &#39; 元&#39;).ljust(15) + &#39;数量:&#39; + number)
       lines = open(&#39;product_list.txt&#39;, &#39;r&#39;).readlines()
       for line in lines: # 修改商品库存
        if commodity in line.split(&#39;, &#39;):     # 定位到商品所在行
         j = lines.index(line)       # 获取商品所在行的行号索引
         select = line.split(&#39;, &#39;)      # 商品所在行分割为字符串列表
         select[-1] = (str(int(select[-1]) - int(number)) + &#39;\n&#39;) # 修改商品库存
         lines[j] = &#39;, &#39;.join(select)      # 将修改后的字符串列表组成字符串
         open(&#39;product_list.txt&#39;, &#39;w&#39;).writelines(lines) # 把修改写回文件
       i += 1

      lines = open(&#39;user_list.txt&#39;, &#39;r&#39;).readlines()

      for line in lines: # 用户余额写入文件
       if username in line.split(&#39;,&#39;):
        j = lines.index(line)
        select = line.split(&#39;,&#39;)
        select[-2] = str(user_balance)
        lines[j] = &#39;,&#39;.join(select)
        open(&#39;user_list.txt&#39;, &#39;w&#39;).writelines(lines)

      exit(&#39;\n合计:%d 元, 账户余额:%d 元&#39; % (total_sum, user_balance))

   # 不结算
   else: 
    print(&#39;\n您有一笔未支付订单...&#39;)
    while True:
     choice = input(&#39;\n是否进行支付?(Y/N):&#39;)
     if not (choice == &#39;Y&#39; or choice == &#39;y&#39; or choice == &#39;N&#39; or choice == &#39;n&#39;):
      print(&#39;\n输入有误,请重试...&#39;)
      continue
     break
    if choice == &#39;n&#39; or choice == &#39;N&#39;:
     exit(&#39;\n订单已取消,感谢光临购物商城,再见...&#39;)
    else:
     if_buy = &#39;Y&#39;
     continue

   if not if_payed:
    print(&#39;\n您有一笔未支付订单...&#39;)
    while True:
     choice = input(&#39;\n是否进行支付?(Y/N):&#39;)
     if not (choice == &#39;Y&#39; or choice == &#39;y&#39; or choice == &#39;N&#39; or choice == &#39;n&#39;):
      print(&#39;\n输入有误,请重试...&#39;)
      continue
     break

    if choice == &#39;n&#39; or choice == &#39;N&#39;:
     exit(&#39;\n订单已取消,感谢光临购物商城,再见...&#39;)
    else:
     if_buy = &#39;Y&#39;
     continue

# 商家入口
if entrance == &#39;2&#39;:  

 seller_name = &#39;&#39;

 # 登录接口
 count = 0
 while count < 3:
  seller_name = input(&#39;\n用户名:&#39;)

  with open(&#39;lock_list.txt&#39;, &#39;r&#39;) as lock_file:
   for lock_line in lock_file:
    if seller_name == lock_line.strip(&#39;\n&#39;):
     exit(&#39;\n用户名 %s 已被锁定,请联系管理员...&#39; % seller_name)

  seller_file = open(&#39;seller_list.txt&#39;, &#39;r&#39;)
  login = False

  for seller_line in seller_file:
   (seller, passwd) = seller_line.strip(&#39;\n&#39;).split(&#39;,&#39;)
   if seller_name == seller:
    n = 0
    while n < 3:
     password = getpass.getpass(&#39;密码:&#39;)
     # 登录成功,跳出while循环
     if password == passwd:
      print(&#39;\n欢迎 %s 登录商城&#39; % seller_name)
      login = True
      break 
     else:
      if n != 2:
       print(&#39;\n密码错误,请重试,您还有 %d 次机会&#39; % (2-n))
     n += 1
    # n = 3,锁定用户名
    else:   
     open(&#39;lock_list.txt&#39;, &#39;w&#39;).write(seller_name + &#39;\n&#39;)
     exit(&#39;\n错误次数过多,账户已被锁定...&#39;)
    # 登录成功,跳出for循环
    if login:  
     break

  # 用户名不存在
  else:  
   if count != 2:
    print(&#39;\n用户名不存在,请重试,您还有 %d 次机会&#39; % (2-count))

  # 登录成功,跳出while循环
  if login: 
   break

  count += 1

 else:
  exit(&#39;\n错误次数过多,程序已退出...&#39;)


 # 商品列表编辑程序
 
 L = []
 # 存放商品列表,初始为空
 commodity_list = []
 index = 1

 print(&#39;\n您的货架上有以下商品:\n&#39;)
 
 print_product_list()

 while True:
  choice = input(&#39;\n是否编辑您的商品列表?(Y/N):&#39;)
  if not (choice == &#39;Y&#39; or choice == &#39;y&#39; or choice == &#39;N&#39; or choice == &#39;n&#39;):
   print(&#39;\n输入有误,请重试...&#39;)
   continue
  break

 if choice == &#39;Y&#39; or choice == &#39;y&#39;:
  while True:
   print(&#39;\n请选择(输入 q 退出):\n&#39;)
   print(&#39;1. 上架新品\n\n2. 下架商品\n\n3. 修改商品信息&#39;)
   choice = input(&#39;\n>>>&#39;)
   if not (choice == &#39;1&#39; or choice == &#39;2&#39; or choice == &#39;3&#39; or choice == &#39;q&#39;):
    print(&#39;输入有误,请重试...&#39;)
    continue

   # 上架新品
   if choice == &#39;1&#39;:
    while True:
     if_add = False # 是否添加商品的标志,初始为False
     new_commodity = input(&#39;\n输入商品名:&#39;)

     product_file = open(&#39;product_list.txt&#39;, &#39;r&#39;)

     for product_line in product_file:
      commodity = product_line.strip(&#39;\n&#39;).split(&#39;, &#39;)[0]  # 获取商品列表中的商品名
      if new_commodity == commodity:
       print(&#39;\n此商品已在货架上...&#39;)
       continue
      else:
       while True:
        if_sure = input(&#39;\n确定上架新品 %s 吗?(Y/N):&#39; % new_commodity)
        if not (if_sure == &#39;Y&#39; or if_sure == &#39;y&#39; or if_sure == &#39;N&#39; or if_sure == &#39;n&#39;):
         print(&#39;\n输入有误,请重试...&#39;)
         continue
        break
       # 确定上架新品
       if if_sure == &#39;Y&#39; or if_sure == &#39;y&#39;:
        while True:  # 输入单价
         price = input(&#39;\n请输入单价:&#39;)
         if not price.isdigit():
          print(&#39;\n输入有误,请重试...&#39;)
          continue
         break
        while True:  # 输入库存
         stock = input(&#39;\n请输入库存:&#39;)
         if not stock.isdigit():
          print(&#39;\n输入有误,请重试...&#39;)
          continue
         break
        new_line = &#39;\n&#39; + new_commodity + &#39;, &#39; + price + &#39;, &#39; + stock
        open(&#39;product_list.txt&#39;, &#39;a&#39;).writelines(new_line)
        print(&#39;\n成功上架新品 %s ,单价 %s 元,库存 %s 件&#39; % (new_commodity, price, stock))

        while True:
         option = input(&#39;\n是否继续添加?(Y/N):&#39;)
         if not (option == &#39;Y&#39; or option or option == &#39;N&#39; or option == &#39;n&#39;):
          print(&#39;\n输入有误,请重试...&#39;)
          continue
         break
        if option == &#39;Y&#39; or option == &#39;y&#39;:
         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 == &#39;2&#39;:
    while True:
     del_num = input(&#39;\n请输入您想下架商品的序号:&#39;)
     if not (del_num.isdigit() or int(del_num) > 0 and int(del_num) <= len(commodity_list)):
      print(&#39;\n输入有误,请重试...&#39;)
      continue
     break

    del_num = int(del_num)
    del_commodity = commodity_list[del_num - 1][0]

    with open(&#39;product_list.txt&#39;, &#39;r&#39;) as old_file:
     with open(&#39;product_list.txt&#39;, &#39;r+&#39;) as new_file:

      current_line = 0

      # 定位到需要删除的行
      while current_line < (del_num - 1):
       old_file.readline()
       current_line += 1

      # 当前光标在被删除行的行首,记录该位置
      seek_point = old_file.tell()

      # 设置光标位置
      new_file.seek(seek_point, 0)

      # 读需要删除的行,光标移到下一行行首
      old_file.readline()
      
      # 被删除行的下一行读给 next_line
      next_line = old_file.readline()

      # 连续覆盖剩余行,后面所有行上移一行
      while next_line:
       new_file.write(next_line)
       next_line = old_file.readline()

      # 写完最后一行后截断文件,因为删除操作,文件整体少了一行,原文件最后一行需要去掉
      new_file.truncate()

    print(&#39;\n您已成功下架 %s !&#39; % del_commodity)

   # 修改商品信息
   elif choice == &#39;3&#39;:

    # 修改商品信息
    def mod_commodity_info(i, j):
     i = int(i)
     j = int(j)
     with open(&#39;product_list.txt&#39;, &#39;r+&#39;) as f:
      current_line = 0
      while current_line < i - 1:
       f.readline()
       current_line += 1
      seek_point = f.tell()
      f.seek(seek_point, 0)

      # 修改商品名
      if j == 1:
       update_line = mod_name() + &#39;, &#39; + commodity_list[i-1][1] + &#39;, &#39; + commodity_list[i-1][2] + &#39;\n&#39;
      # 修改商品价格
      elif j == 2:
       update_line = commodity_list[i-1][0] + &#39;, &#39; + mod_price() + &#39;, &#39; + commodity_list[i-1][2] + &#39;\n&#39;
      # 修改商品库存
      else:
       update_line = commodity_list[i-1][0] + &#39;, &#39; + commodity_list[i-1][1] + &#39;, &#39; + mod_stock() + &#39;\n&#39;
      
      f.write(update_line)
     return
    
    def mod_name():
     new_name = input("\n请输入新的商品名:")
     return new_name

    def mod_price():
     new_price = input("\n请输入新的商品单价:")
     return new_price

    def mod_stock():
     new_stock = input("\n请输入新的商品库存:")
     return new_stock

    # 修改商品单价
    def mod_commodity_price(i):
     i = int(i)
     with open(&#39;product_list.txt&#39;, &#39;r+&#39;) as f:
      current_line = 0
      while current_line < i -1:
       f.readline()
       current_line += 1
      seek_point = f.tell()
      f.seek(seek_point, 0)
      new_price = input()


    while True:
     i = input("\n请输入需要编辑的商品序号(输入 c 取消):")
     if not (i.isdigit or i == &#39;c&#39; or int(i) > 0 and int(i) <= len(commodity_list)):
      print("\n输入有误,请重试...")
      continue
     elif i == &#39;c&#39;:
      break
     else:
      while True:
       j = input("\n请选择需要编辑的选项(输入 c 取消):\n\n1. 商品名\n\n2. 单价\n\n3. 库存\n\n>>>")
       if not (j == &#39;c&#39; or j == &#39;1&#39; or j == &#39;2&#39; or j == &#39;3&#39;):
        print("\n输入有误,请重试...")
        continue
       break
      if j == &#39;c&#39;:
       break
      else:
       mod_commodity_info(i, j)

   else:
    exit(&#39;\n您已退出商城...&#39;)
 else:
  exit(&#39;\n您已退出商城...&#39;)
Copy after login

The above is the detailed content of Python implements a shopping mall with user and merchant entrances. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template