Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86498
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-12 22:31
文章分类

全部博文(26)

文章存档

2016年(26)

我的朋友

分类: Python/Ruby

2016-11-19 22:48:06


点击(此处)折叠或打开

  1. #_*_ coding:utf-8 _*_
  2. import os, sys, time
  3. #-----------------------------------登录验证-------------------------------------------
  4. if os.path.isfile('user.txt'):
  5.     pass
  6. else:
  7.     sys.exit("\033[31m没有定义用户文件\033[0m")
  8. user_li = []
  9. with open('user.txt') as f1: #提取用户文件,列表化
  10.     for i in f1:
  11.        user = i.strip('\n').split(':')
  12.        user_li.append(user)

  13. def user_name(): #用户名列表
  14.     user_n = []
  15.     for name in user_li:
  16.         user_n.append(name[0])
  17.     return user_n

  18. def user_passwd(): #密码列表
  19.     user_pass = []
  20.     for passwd in user_li:
  21.         user_pass.append(passwd[1])
  22.     return user_pass

  23. def login_code(): #定义登录验证函数
  24.     while True:
  25.         user_na = input('请输入用户名:')
  26.         username = user_na.strip()
  27.         password = input('请输入密码:')
  28.         if username in user_name():
  29.             location = user_name().index(username)
  30.             while int(user_li[location][2]) < 3:
  31.                 if password == user_li[location][1]:
  32.                     return username
  33.                 else:
  34.                     location = user_name().index(username)
  35.                     user_li.insert(location,[username,user_li[location][1],\
  36.                     str(int(user_li[location][2])+1),user_li[location][3]])
  37.                     user_li.pop(location+1)
  38.                     with open('user.txt','w+') as f1:
  39.                         for i in user_li:
  40.                             s1 = ':'.join(i)
  41.                             f1.write(s1+'\n')
  42.                     print ("\033[31m账户名或密码错误\033[0m")
  43.                     break
  44.             else:
  45.                 print ("\033[31m账户已经锁定,请联系管理员!\033[0m")
  46.                 sys.exit(0)
  47.         else:
  48.             print ("\033[31m账户名或密码错误\033[0m")
  49. #--------------------------------------------------------------------------------------------------------
  50. def shop_list(): #打印出商品的编码,商品名称,价格
  51.     for m, n in enumerate(trade_dict, 1):
  52.         print('\t%s\t%s\t%s'%(m, n, trade_dict[n]))

  53. def trade_price(trade_code): #通过输入的编码来找到商品的价格
  54.     for x,y in enumerate(trade_dict, 1):
  55.         if x == trade_code:
  56.             return trade_dict[y]

  57. def trade_name(trade_code): #通过输入编码来找到商品的名称
  58.     for x, y in enumerate(trade_dict, 1):
  59.         if x == trade_code:
  60.             return y

  61. def trade_list_num(): #统计字典中的编码,列表化,为后面判断商品编码准备
  62.     trade_n = []
  63.     for m, n in enumerate(trade_dict, 1):
  64.         trade_n.append(m)
  65.     return trade_n

  66. def result_print(): #格式化打印购买商品的信息
  67.     li1 = []
  68.     for i in trade_bill:
  69.         li2 = []
  70.         for m in i:
  71.             li2.append(str(m))
  72.         li1.append(li2)
  73.     for x in li1:
  74.         s1 = ' '.join(x)
  75.         print(s1+'\n')
  76. #----------------------------------------------------------------------------------------------------------
  77. user1_name = login_code() #获取当前登录状态下的用户名

  78. def continue_yn(): #定义是否返回上一级菜单函数
  79.     while True:
  80.         res_code1 = input("\033[32m'b'(返回上一级),'q'(退出):\033[0m")
  81.         if res_code1 == 'b':
  82.             break
  83.         elif res_code1 == 'q':
  84.             sys.exit('ByeBye')
  85.         else:
  86.             print('\033[31m错误的输入,请重新输入:\033[0m')

  87. choice_li = ['查看账户余额','充值','查看历史购物信息','开始购物']
  88. while True:
  89.     for x, y in enumerate(choice_li, 1):
  90.         print(x, y)
  91.     choice = input('\033[32m------>请输入你的选择(编码):\033[0m')
  92.     if choice == '1':
  93.         for x in user_li:
  94.             if x[0] == user1_name:
  95.                 print(x[3])
  96.         continue_yn()

  97.     elif choice == '2':
  98.         charge_num = input('请输入充值金额:')
  99.         for x in user_li:
  100.             if x[0] == user1_name:
  101.                 print('余额:',x[3])
  102.                 location1 = user_li.index(x)
  103.                 user_li.insert(location1, [x[0], x[1], x[2], str(int(x[3]) + int(charge_num))])
  104.                 user_li.pop(location1 + 1)
  105.                 print('充值后金额:',user_li[location1][3])
  106.         with open('user.txt', 'w+') as f1:
  107.             for i in user_li:
  108.                 s1 = ':'.join(i)
  109.                 f1.write(s1 + '\n')
  110.         continue_yn()

  111.     elif choice == '3':
  112.         if os.path.isfile(user1_name+'_shopping_history'):
  113.             with open(user1_name+'_shopping_history') as f1:
  114.                 for i in f1:
  115.                     print(i)
  116.         else:
  117.             print('没有购物的历史记录')
  118.         continue_yn()

  119.     elif choice == '4':
  120.         print('欢迎来到super购物中心'.center(50, '*'))
  121.         break
  122.     else:
  123.         print('\033[31m错误的输入\033[0m')

  124. if os.path.isfile('menu_info.txt'):
  125.     pass
  126. else:
  127.     sys.exit("\033[31m没有定义menu_info.txt\033[0m")

  128. usr_dic = {}
  129. with open('menu_info.txt') as f1: #从商品文件menu_info账户中取出商品信息,字典化
  130.     for i in f1:
  131.         area_dic = {}
  132.         l1 = i.split(':')
  133.         l2 = l1[1].strip('\n').split(' ')
  134.         d1 = {}
  135.         for i in l2:
  136.             l3 = i.split('|')
  137.             l4 = l3[1].split(';')
  138.             d2 = {}
  139.             for m in l4:
  140.                 d3 = {}
  141.                 l5 = m.split('*')
  142.                 l6 = l5[1].split(',')
  143.                 for x in l6:
  144.                     l7 = x.split('.')
  145.                     d3[l7[0]] = l7[1]
  146.                 d2[l5[0]] = d3
  147.             d1[l3[0]] = d2
  148.         usr_dic[l1[0]] = d1

  149. def lay_mem(dic_name):#打印分级菜单,生成编码列表,为下面判断编码准备
  150.     li_name = []
  151.     for x, y in enumerate(dic_name, 1):
  152.         print(x,y)
  153.         li_name.append(x)
  154.         a = 3
  155.     return li_name

  156. def input_code():
  157.     code_name = input("\033[32m请输入->'商品编码','b','q'(b:返回上一级 q:退出):\033[0m")
  158.     return code_name

  159. for x in user_li: #取出salary值,即用户账户余额
  160.     if x[0] == user1_name:
  161.         salary = int(x[3])
  162. sum_comsume = 0 #消费总额初始化
  163. trade_bill = []
  164. while True:
  165.     result1 = lay_mem(usr_dic) #打印商品一级菜单
  166.     city_code = input("\033[32m请输入->'商品编码','q'(q:退出):\033[0m")
  167.     if city_code in str(result1):
  168.         for m, n in enumerate(usr_dic, 1):
  169.             if m == int(city_code):
  170.                 while True:
  171.                     result2 = lay_mem(usr_dic[n])
  172.                     res_code = input_code()
  173.                     if res_code in str(result2):#打印商品二级菜单
  174.                         for x,y in enumerate(usr_dic[n],1):
  175.                             if res_code == str(x):
  176.                                 while True:
  177.                                     result3 = lay_mem(usr_dic[n][y])
  178.                                     town_code = input_code()
  179.                                     if town_code in str(result3): #打印商品三级菜单
  180.                                         for u, v in enumerate(usr_dic[n][y],1):
  181.                                             if u == int(town_code):
  182.                                                  trade_dict = (usr_dic[n][y][v])
  183.                                                  while True:
  184.                                                      shop_list()
  185.                                                      trade_code = input("\033[32m请输入->'购买商品编码','b'(b:返回上一级):\033[0m")
  186.                                                      if trade_code in str(trade_list_num()): # 判断输入的商品编码是否有误
  187.                                                          price = trade_price(int(trade_code))
  188.                                                          name = trade_name(int(trade_code))
  189.                                                          while True: #判断输入商品数量是否合法,是否为数值型
  190.                                                              trade_nu = input('\033[32m请输入购买商品数量:\033[0m')
  191.                                                              if trade_nu.isdigit():
  192.                                                                  trade_num = int(trade_nu)
  193.                                                                  break
  194.                                                              else:
  195.                                                                  print('\033[31m输入错误请重新输入:\033[0m')

  196.                                                          balance = salary - int(price)*trade_num
  197.                                                          if balance >= 0: # 当余额大于零时才可以购买商品
  198.                                                              s1 = '''\t%s\t%s\t%s \n''' % (
  199.                                                              name, trade_num, int(price)*trade_num)
  200.                                                              sum_comsume = sum_comsume + int(price)*trade_num # (消费的总额)
  201.                                                              salary = salary - int(price)*trade_num # 余额
  202.                                                              # ---------------更新账单,反复购买一件商品会进行自动累加------------------------------------------------
  203.                                                              trade_li = [name, trade_num, int(price)*trade_num]
  204.                                                              code_li = []
  205.                                                              for i in trade_bill:
  206.                                                                  code_li.append(i[0])
  207.                                                              if trade_li[0] in code_li: # 判断新输入的商品信息是否已经存在,存在则进行累加
  208.                                                                  location = code_li.index(trade_li[0])
  209.                                                                  trade_new_li = ([trade_li[0],
  210.                                                                                   trade_bill[location][1] +
  211.                                                                                   trade_li[1],
  212.                                                                                   trade_bill[location][2] +
  213.                                                                                   trade_li[2]])
  214.                                                                  trade_bill.append(trade_new_li)
  215.                                                                  trade_bill.pop(location)
  216.                                                              else: # 如果,不存在,直接追加到购买的商品列表的后面
  217.                                                                  trade_bill.append(trade_li)
  218.                                                              while True:
  219.                                                                  final_state = input(
  220.                                                                      '\033[32m请输入下一步操作--> y/n(Y/N)(是否结算);c/C (查看购物信息):\033[0m')
  221.                                                                  if final_state == 'Y' or final_state == 'y':
  222.                                                                      s3 = '%s购物清单'%(user1_name)
  223.                                                                      print(s3.center(50, '*'))
  224.                                                                      s2 = '''消费总额:%s\t账户余额:%s\t消费时间:%s\t''' % \
  225.                                                                           (sum_comsume, salary,
  226.                                                                            time.strftime('%Y-%m-%d %H:%M:%S'))
  227.                                                                      result_print()
  228.                                                                      print('-'.center(50, '-'))
  229.                                                                      print(s2)
  230.                                                                      #---------把购物历史记录写入以用户名命名的文件----
  231.                                                                      with open(user1_name + '_shopping_history',
  232.                                                                                'a+') as f3:
  233.                                                                          li1 = []
  234.                                                                          for i in trade_bill:
  235.                                                                              li2 = []
  236.                                                                              for m in i:
  237.                                                                                  li2.append(str(m))
  238.                                                                              li1.append(li2)
  239.                                                                          for x in li1:
  240.                                                                              s1 = ' '.join(x)
  241.                                                                              # print(s1 + '\n')
  242.                                                                              f3.write(s1+'\n')
  243.                                                                      with open(user1_name + '_shopping_history',
  244.                                                                                    'a+') as f3:
  245.                                                                          f3.write(s2+'\n')
  246.                                                                          f3.write('-'.center(50,'-')+'\n')
  247.                                                                      #--------------在文件中更新用户的余额--------
  248.                                                                      for x in user_li:
  249.                                                                          if x[0] == user1_name:
  250.                                                                              location2 = user_li.index(x)
  251.                                                                              user_li.insert(location2,
  252.                                                                                             [x[0], x[1], x[2], str(salary)])
  253.                                                                              user_li.pop(location2 + 1)
  254.                                                                      with open('user.txt', 'w+') as f1:
  255.                                                                          for i in user_li:
  256.                                                                              s1 = ':'.join(i)
  257.                                                                              f1.write(s1 + '\n')
  258.                                                                      sys.exit(0)
  259.                                                                  elif final_state == 'n' or final_state == 'N':
  260.                                                                      break
  261.                                                                  elif final_state == 'c' or final_state == 'C':
  262.                                                                      result_print()
  263.                                                                      print('\033[32m消费:%s\t余额:%s\033[0m'%(sum_comsume,salary))
  264.                                                                  else:
  265.                                                                      print('\033[31m错误的输入请重新输入!\033[0m')
  266.                                                                      continue
  267.                                                          else:
  268.                                                              print("\033[31m余额不足,请重新选择商品\033[0m")
  269.                                                      elif trade_code == 'b':
  270.                                                          break
  271.                                                      else:
  272.                                                          print('\033[31m商品编码输入有误,请重新输入\033[0m')
  273.                                                          continue
  274.                                     elif town_code == 'b':
  275.                                         break
  276.                                     elif town_code == 'q':
  277.                                         sys.exit('\033[32mByeBye\033[0m')
  278.                                     else:
  279.                                         print ('\033[31m非法输入,请重新输入!\033[0m')
  280.                     elif res_code == 'b':
  281.                         break
  282.                     elif res_code == 'q':
  283.                         sys.exit('\033[32mByeBye\033[0m')
  284.                     else:
  285.                         print ('\033[31m非法输入,请重新输入!\033[0m')
  286.     elif city_code == 'q':
  287.         sys.exit('\033[32mByeBye\033[0m')
  288.     else:
  289.         print ("\033[31m非法输入,请重新输入!\033[0m")


[yn@localhost@../menu]$cat menu_info.txt 
手机数码:热牌手机|iphone*iphone7.5899,iphone6s.3799;OPPO*OPPO-R9s.2799,OPPO-R9.2499 摄影摄像|单反相机*佳能.7299,尼康.2799;摄像机*索尼.2980,乐道.388
潮流女装:热卖品牌|新品连衣裙*伊莲娜冬款.338,范依琪冬款.138;针织开衫*姐弟女装.400,韩都衣舍.194 特色类目|旗袍*华族经典.299,亲晨秋款.216;礼服*创意狐.1288,希洛莎.298
[yn@localhost@../menu]$cat user.txt 
yn:yn123:0:50000
alex:123456:0:50000
tom:tom123:0:50000
sky:123:0:50000
[yn@localhost@../menu]$

阅读(1497) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~