Python小程序之购物车

需求:

  用户入口:

  1、商品信息放在文件中,从文件中读取

  2、已购商品,余额记录,第一要输入起始金额,以后不需要二次输入

  商家入口:

  2、可以添加商品,修改商品价格

  1 # Author:Lee Sirimport sys
  2 
  3 
  4 def goods_info():
  5     goods_info = 'goods_info.txt'
  6     fd = open(goods_info)
  7     return fd
  8 
  9 def goods_list(fd):
 10     print('商品列表'.center(30,'-'))
 11     for items in fd.readlines():
 12         index,goods_name,goods_price = items.split()
 13         print(index,goods_name,goods_price)
 14 
 15 def check_cart(buylist):
 16     print('您的购物车如下:')
 17     for name,price in buylist:
 18         print('名称:',name,'价格:',price)
 19     check_choice = input('是否结账?:[Y/N]')
 20     if check_choice == 'y' or check_choice == 'Y':
 21         return True
 22     else:
 23         return False
 24 
 25 def check_money(money,buylist):
 26     money = int(money)
 27     chech_result = 0
 28     for name,price in buylist:
 29         chech_result += int(price)
 30     if chech_result <= money:
 31         return True,money - chech_result
 32     else:
 33         return False,money
 34 
 35 
 36 def buyer():
 37     buylist = []
 38     while True:
 39         money = input("请输入的金钱:")
 40         if money.isdigit():
 41             while True:
 42                 money = int(money)
 43                 fd = goods_info()
 44                 goods_list(fd)
 45                 choice = input('请输出你要购买的商品序号:')
 46                 if choice.isdigit():
 47                     choice = int(choice)
 48                     # print(type(choice))
 49                     fd.seek(0)
 50                     if choice > 0  and choice <= len(fd.readlines()):
 51                         fd.seek(0)
 52                         #print('1')
 53                         for items in fd.readlines():
 54                             index, goods_name, goods_price = items.split()
 55                             if int(index) == choice:
 56                                 buylist.append((goods_name,goods_price))
 57                                 print('添加 %s 至购物车。' % (goods_name))
 58                                 buy_choice = input('是否继续购买? [Y/N]')
 59                                 if buy_choice == 'y' or buy_choice == 'Y':
 60                                     continue
 61                                 else:
 62                                     if len(buylist) > 0:
 63                                         if check_cart(buylist):
 64                                             check_result, balance = check_money(money,buylist)
 65                                             if check_result:
 66                                                 exit('结算成功,您的余额还有 %s' % balance )
 67                                             else:
 68                                                 continue_buy = input('您的余额不足,是否修改购物车?: [Y/N]')
 69                                                 if continue_buy == 'Y' or continue_buy == 'y':
 70                                                     print('您的购物车如下:')
 71                                                     for index,buy_name in enumerate(buylist):
 72                                                         name,price = buy_name
 73                                                         print('序号:',index,'名称:', name, '价格:', price)
 74                                                     else:
 75                                                         while True:
 76                                                             dele_cart = input('请输入你要删除的产品序号:')
 77                                                             if dele_cart.isdigit():
 78                                                                 dele_cart = int(dele_cart)
 79                                                                 if dele_cart >= 0 and dele_cart <= len(buylist):
 80                                                                     buylist.remove(dele_cart)
 81                                                                     break
 82                                                                 else:
 83                                                                     print('输入错误请重新输入')
 84                                                             else:
 85                                                                 print('输入错误,请重新输入')
 86 
 87                                     else:
 88                                         continue_check = input('您的购物车为空,是否继续购买? [Y/N]')
 89                                         if continue_check == 'Y' or continue_check == 'y':
 90                                             continue
 91                                         else:
 92                                             exit('See you tomorrow!')
 93                     else:
 94                         print('输入有误,请重新输入:')
 95 
 96                 else:
 97                     exit('您没有购买任何产品,再见!')
 98         else:
 99             print('输入有误,请重新输入')
100 
101 
102 buyer()
原文地址:https://www.cnblogs.com/dachenzi/p/6523279.html