• python第七天-作业[购物车]


    作业要示:

    购物车程序:
    启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    可随时退出,退出时,打印已购买商品和余额
    在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
    用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
    允许查询之前的消费记录

    先上一下流程吧!

    具体代码还在优化中》》》先一下与流程图对应的代码吧!

      1 #!usr/bin/env python
      2 #-*-coding:utf-8-*-
      3 # Author calmyan
      4 import time,sys,os
      5 with open('user_file.txt','r',encoding='utf-8') as user_file:
      6     user_list=eval(user_file.readline())#转换用户文件中的字符串
      7 lock_name=open('lock_name.txt','r+',encoding='utf-8')#打开锁定用户列表文件
      8 lock_list=eval(lock_name.readline())#转换锁定用户文件中的字符串
      9 
     10 def userchar(_user_name):#检测用户名的函数
     11     while True:
     12         if _user_name in user_list and _user_name not in lock_list:#判断用户是否存在并正常
     13             return True#存在返回真
     14             break
     15         elif _user_name in user_list and _user_name  in lock_list:#判断用户是否被锁定
     16             print('您的用户已经被锁定,请联系商家解锁!')
     17             break
     18         else:
     19             print('您输入用户不存在,请先注册')
     20             break
     21 
     22 def confirm():#退出确认函数
     23     qu=input("是否退出登陆!按33[31;1mY/y33[0m退出? 按其它键重新登陆:")
     24     if qu=='Y'or qu=='y':
     25         print("您已经退出!" )
     26         exit()#退出程序
     27     else :
     28         pass
     29 
     30 def user_pass():
     31     bool=True#定义退出循环条件变量
     32     while bool:
     33         _user_name=(input('请输入用户名:'))
     34         if userchar(_user_name.strip()):#运行检测用户名的函数判断,去除两边空格
     35             count=3#定义锁次数
     36             while count>0 :#如果密码错误次数超过退出密码输入
     37                 #password=getpass.getpass('请输入密码:')
     38                 password=input('请输入密码:')
     39                 if password==user_list[_user_name]:#密码通过验证结束循环
     40                     print("密码通过验证")
     41                     bool=False
     42                     print("欢迎您  33[32;1m%s33[0m  您已经登陆成功!" % _user_name)
     43                     return _user_name#返回用户名
     44                 else:
     45                     count -=1#次数减少
     46                     print("密码错误请重试,剩余:33[31;1m%s33[0m次! "% count)
     47                     pass
     48             else:
     49                 print("33[31;1m超出所试次数,账户已锁定!请联系商家!33[0m")
     50                 lock_list.append(_user_name)#将用户名加出锁定列表
     51                 lock_name.seek(0)#指针移动到开头,以免产生多个元组
     52                 lock_name.write(str(lock_list))#写入锁定用户文件
     53                 lock_name.tell()#获取当前指针,以免产生多个元组
     54                 lock_name.close()#关闭锁定用户文件
     55                 confirm()#退出确认函数
     56                 #continue#退回到输入用户名的位置
     57         else:#如果用户名出错新来过
     58             confirm()
     59             pass
     60 
     61 def infor():#打开用户信息函数
     62     with open('user_infor','r+',encoding='utf-8') as user_infor:#打开用户购物信息文件
     63         infor_list=eval(user_infor.readline())#转换文件的字符串
     64         return infor_list
     65 
     66 #程序开始:
     67 if os.path.exists('history'):#确认文件是否存在
     68     pass
     69 else:
     70     with open('history','w',encoding='utf-8') as user_history:#创建一个用户帐户购物记录文件
     71         user_history.write('{}')#写入空值
     72     pass
     73 
     74 if os.path.exists('user_infor'):#确认文件是否存在
     75     pass
     76 else:
     77     with open('user_infor','w',encoding='utf-8') as user_infor:#创建一个用户帐户信息记录文件
     78         user_infor.write('{}')#写入空值
     79     pass
     80 
     81 print("欢迎来到33[31;1mpython33[0m购物商城!")
     82 time_format='%y-%m-%d %X'#定义时间格式
     83 times=time.strftime(time_format)#定义时间
     84 _user_name= user_pass()#将通过验证的用户名赋于变量
     85 
     86 if _user_name in infor():#如果用户信息存在记录就跳过
     87     pass
     88 else:
     89     while True:
     90         name_char=input("首次登陆,请输入工资(购物资金):")#用户输入工资(购物资金)
     91         if  name_char.isdigit():#检测输入是否为整数
     92             name_char=int(name_char)#转为int
     93             with open('history','r+',encoding='utf-8') as user_history:#打开用户帐户购物记录文件
     94                 history_list=eval(user_history.readline())#将文本读为字典
     95                 history_list[_user_name]={}#创建用户购物记录
     96                 user_history.seek(0)#移到开头
     97                 user_history.write(str(history_list))#写入文件
     98             with open('user_infor','r+',encoding='utf-8') as user_infor:
     99                 infor_list=eval(user_infor.readline())#转换首登用户文件中的字符串
    100                 infor_list[_user_name]={"金额":name_char}
    101                 user_infor.seek(0)
    102                 user_infor.write(str(infor_list))#写入用户信息
    103             break
    104         else:
    105             print('金额不能为0或负数请正确输入金额!')
    106             continue
    107 #再次登陆直接显示用户信息
    108 infor_list=infor()#获取用户信息字典
    109 name_char=int(infor_list[_user_name]['金额'])#获取余额
    110 with open('history','r',encoding="utf-8") as user_history:#读取用户文件
    111     history_list=eval(user_history.readline())#用户购物信息
    112 shoplist=[]#购物车列表
    113 shop_count=0#购物金额统计
    114 ###进入商品列表###
    115 goodsname=[]#定义商品列表
    116 print('用户名:33[32;1m%s33[0m:'%_user_name)#输出用户信息
    117 print('您的余额:33[31;1m%s33[0m:'%name_char)
    118 history=history_list[_user_name]#用户购物记录信息
    119 print('您的购物记录如下:')
    120 for k,v in history.items():#转为列表进行输出
    121     print(k,v)#输出购物记录
    122 print('加载商品列表:')
    123 #加载动态
    124 for i in range(40):
    125     sys.stdout.write('#')
    126     sys.stdout.flush()
    127     time.sleep(0.05)
    128 print('
    ')
    129 
    130 
    131 with open('goodslist.txt','r',encoding='utf-8') as f:#打开商品列表文件
    132     for line in f:
    133         a=line.find(',')#定位分割符的索引进行分割
    134         goodsname.append((line[0:a],int(line[a+1:])))#元组模式追加到列表,价格转为整数
    135 
    136 while True:
    137     for index,p_item in enumerate(goodsname):#取出下标输出 商品列表
    138         print(index,p_item)#输出商品列表
    139     user_index=input('请输入商品编号添加你所在购买的商品,按用33[31;1mq/Q33[0m退出>>>:')
    140     if user_index.isdigit():#判断是否为整数
    141         user_index=int(user_index)#转为数字
    142         if user_index < len(goodsname) and user_index>=0:#判断所选商品编号在在列表
    143             p_item=goodsname[user_index]#读取下标所在(元素)商品
    144             if p_item[1]<=name_char:#余额是否足够
    145                 shoplist.append(p_item)#添加到购物车
    146                 name_char-=p_item[1]#扣除金额
    147                 shop_count+=p_item[1]#累加购物金额
    148                 print("添加 33[32;1m%s33[0m 到购物车,价格: 33[32;1m%s33[0m 元"% p_item)
    149                 print("你的购物金额余 33[31;1m%s33[0m 元"% name_char)
    150             else:
    151                 print("你的购物金额余 33[41;1m%s33[0m 元,无法购买该商品!" % name_char)
    152         else:
    153              print('输入错误,该编号 33[41;1m%s33[0m 的商品不存在!'%user_index)
    154     elif user_index=='q'or user_index=='Q':
    155         print('你的购物清单:')
    156         for up in shoplist:#打印购物清单
    157             print(up)
    158         print("你的购物总金额为:33[32;1m%s33[0m 元!"% shop_count)
    159         print("你的购物金额余: 33[31;1m%s33[0m 元!" % name_char)
    160         while True:
    161             confirm=input("你是否确定退出购物?33[31;1m Y/y 33[0m 是 33[31;1mN/n33[0m 否:")
    162             if confirm== 'Y'or confirm== 'y' :
    163                 infor_list[_user_name]['金额']=name_char#修改用户金额(购物后)
    164                 shoplist.append(('总额:',shop_count))#列表加入总额
    165                 history_list[_user_name][times+'购物清单:']=shoplist#添加时间:清单
    166                 with open('user_infor','w',encoding="utf-8") as user_infor:#写入文件用户信息
    167                     user_infor.seek(0)#移到最前
    168                     user_infor.write(str(user_list))#写入
    169                 with open('history','r+',encoding='utf-8') as user_history:#购物信息写入文件
    170                     user_history.seek(0)#移到最前
    171                     user_history.write(str(history_list))#写入
    172                 exit(print("谢谢!欢迎您再次光临!"))
    173             elif confirm=='N'or confirm== 'n' :
    174                 break
    175             else:
    176                 print("输入错误!请按提示输入!")
    177                 continue
    178     else:
    179         print('33[41;1m输入商品编号错误,请重新输入!33[0m')
    View Code

    还着一点点,撸了一天的代码了!!还想要更好!!

    完美 主义,伤不起来!! 

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【莫柔落切】!

    联系或打赏博主【莫柔落切】!https://home.cnblogs.com/u/uge3/

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    107. Binary Tree Level Order Traversal II
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    104. Maximum Depth of Binary Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    系统和进程相关信息
    文件I/0缓冲
    系统编程概念(文件系统mount等函数的使用)
  • 原文地址:https://www.cnblogs.com/uge3/p/6833032.html
Copyright © 2020-2023  润新知