python小项目练习
Posted jinyan-huang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python小项目练习相关的知识,希望对你有一定的参考价值。
写了一天,感觉人要渣。。收获蛮多,但也有不少BUG,先放上来,日后继续学习完善。。。。
""" 功能: 1.用户注册,提示用户输入用户名和密码,然后获取当前注册时间,最后将用户名、密码、注册时间写入到文件。 2.用户登录,只有三次错误机会,一旦错误则冻结账户(下次启动也无法登录,提示:用户已经冻结)。 3.商品浏览,分页显示商品(小文件); 用户可以选择商品且可以选择数量然后加入购物车(在全局变量操作), 不再购买之后,需要讲购物车信息写入到文件,文件要写入到指定目录: shopping_car(文件夹) - 用户名A(文件夹) 2019-11-11-09-59.txt 2019-11-12-11-56.txt 2019-12-11-11-47.txt - 用户B(文件夹) 2019-11-11-11-11.txt 2019-11-12-11-15.txt 2019-12-11-11-22.txt 注意:重复购买同一件商品时,只更改购物车中的数量。 4.我的购物车,查看用户所有的购物车记录,即:找到shopping_car目录下当前用户所有的购买信息,并显示: 2019-11-11-09-59 飞机|1000|10个 大炮|2000|3个 2019-11-12-11-56.txt 迫击炮|10000|10个 手枪|123|3个 5.用户未登录的情况下,如果访问 商品流程 、我的购物车 时,提示登录之后才能访问,让用户先去选择登录(装饰器实现)。 """ from datetime import datetime import os,sys def shoop_warp(func): def inner(*args,**kwargs): if not SUSS_USER: print(‘请先登陆‘) return func(*args,**kwargs) return inner #装饰器判断用户登陆 def user_on_warp(func): def inner(*args,**kwargs): if ERR_USER.get(args[0],0) >=3: print(‘账户已被冻结‘) write_err_user() return elif args[0] in SUSS_USER: print(‘登陆重复‘) return elif len(SUSS_USER) > 0: print(‘已有账号登陆,请注销再登陆,当前账号为%s‘%(SUSS_USER[0])) return func(*args,**kwargs) return inner # 装饰器:判断注册用户是否已被注册 def user_warp(func): ‘‘‘ 1.加载用户文件,返回用户列表 2.判断用户名是否跟用户列表重名,返回False 则退出 return 3.判断用户必须为字母,返回False 则退出 return 4.满足以上条件后,则执行user_log函数 ‘‘‘ def inner(*args,**kwargs): ls = read_user() if ls.count(args[0]): print(‘用户已存在‘) return if args[0].isdigit(): print(‘必须字母‘) return func(*args,**kwargs) return inner #用户注册写入文件 @user_warp #user_log = user_wap(user_log) def user_log(user_name,pwd): ‘‘‘ 1.获取当前注册时间 2.把用户名、密码、时间,拼接起来,用逗号分隔 3.获取文件user.txt文件路径 4.写入user.txt文件 5.执行建立用户的文件夹 ‘‘‘ date = datetime.now().strftime(r‘%Y-%m-%d‘) s = ‘%s,%s,%s\n‘%(user_name,pwd,date) path = os.path.join(os.path.dirname(__file__),‘user.txt‘) with open(path,mode=‘a‘,encoding=‘utf-8‘) as f: f.write(s) create_user_dir(user_name) print(‘创建成功重新执行程序‘) sys.exit() #读取注册用户 def read_user(): ‘‘‘ 1.生成列表 2.读取user.txt文件内容 3.加入列表 4.返回列表 ‘‘‘ ls = [] path = os.path.join(os.path.dirname(__file__),‘user.txt‘) with open(path,mode=‘r‘,encoding=‘utf-8‘) as f: for line in f: l = line.split(‘,‘) ls.append(l[0]) return ls #读取注册用户密码 def read_pwd(): ‘‘‘ 1.生成列表 2.读取user.txt文件内容 3.加入列表 4.返回列表 ‘‘‘ ls = [] path = os.path.join(os.path.dirname(__file__),‘user.txt‘) with open(path,mode=‘r‘,encoding=‘utf-8‘) as f: for line in f: l = line.split(‘,‘) ls.append(l[1]) return ls #创建文件目录 def create_user_dir(user_name): ‘‘‘ 1.获取文件夹绝对路径 2.创建目录名 3.创建目录绝对路径 + 文件名目录 ‘‘‘ path = os.path.join(r‘G:\PT_pro\day16\shopping_car‘,user_name) os.mkdir(path) #用户登陆 @user_on_warp def user_on(user_name,pwd): user_ls = read_user() pwd_ls = read_pwd() for u,p in zip(user_ls,pwd_ls): if user_name == u and pwd == p: print(‘登陆成功‘) SUSS_USER.append(u) return elif user_name == u and pwd != p: print(‘密码错误‘) ERR_USER.setdefault(u,0) ERR_USER[u] +=1 return print(‘用户或密码错误‘) return #写入锁定用户 def write_err_user(): with open(‘err.txt‘,mode=‘w‘,encoding=‘utf-8‘) as f: for k,v in ERR_USER.items(): f.write(‘%s:%s\n‘%(k,v)) f.close() #初始化读取锁定用户 def read_err_user(): with open(‘err.txt‘,mode=‘r‘,encoding=‘utf-8‘) as f: dic = for line in f: ls = line.strip().split(‘:‘) dic[ls[0]] = int(ls[1]) return dic def show_menu(): print(‘‘‘ 1.用户注册 2.用户登陆 3.退出当前账户 4.商品游览 5.我的购物车 ‘‘‘) #退出当前用户 def log_out_user(): SUSS_USER.clear() def read_goods(): with open(‘goods‘,mode=‘r‘,encoding=‘utf-8‘) as f: data = f.readlines() return data @shoop_warp def show_shopping(): count = 5 while True: page = int(input(‘请输入页码‘)) start = (page-1)*count end = (page*count)+1 date = GOODS_LIST[start:end] # if not date: # print(‘超出范围‘) # continue for i in date: print(i) seq = input(‘请输入需要购买的商品序号,输入q进行下一次输入,或输入N退出购买界面:‘) if seq.lower() == ‘q‘: continue elif seq.lower() ==‘n‘: return num = int(input(‘请输入购买的数量:‘)) d=buy_good(seq,num) if d: print(‘购买成功‘) write_good_file(SUSS_USER[0]) def buy_good(seq,num): dic = for good in GOODS_LIST: good = ‘|‘.join(good.split(‘.‘)).strip().split(‘|‘) if seq == good[0]: dic[seq] = good[1] dic[‘price‘] = good[2] dic[‘num‘] = num add_goods(dic,seq,num) return True else: print(‘没有此商品‘) return def add_goods(dic,seq,num): if not BUY_GOODS: BUY_GOODS.append(dic) return for i in range(len(BUY_GOODS)): if seq in BUY_GOODS[i].keys(): BUY_GOODS[i][‘num‘] += num return BUY_GOODS.append(dic) def write_good_file(username): dir_path =os.path.join( r‘G:\PT_pro\day16\shopping_car‘,username) date = datetime.now().strftime(‘%Y-%m-%d‘) path = os.path.join(dir_path,date)+‘.txt‘ with open(path,mode=‘w‘,encoding=‘utf-8‘) as f: for good in BUY_GOODS: f.write(str(good)+‘\n‘) @shoop_warp def my_shop_list(username): abs = os.path.join(‘G:\PT_pro\day16\shopping_car‘,username[0]) re = os.listdir(abs) for i in range(len(re)): path = os.path.join(abs,re[i]) with open(path,mode=‘r‘,encoding=‘utf-8‘) as f: print(re[i]) for line in f: print(line.strip()) #登陆成功的用户 SUSS_USER = [] #锁定用户字典 ERR_USER = read_err_user() #初始化商品 GOODS_LIST = read_goods() #购物车 BUY_GOODS = [] fucn_list = "1":user_log,"2":user_on,"3":log_out_user,"4":show_shopping,"5":my_shop_list while True: show_menu() num = input(‘请输入序号‘) if num == ‘1‘: username = input(‘请输入注册用户‘) pwd = input(‘请输入密码‘) fucn_list[num](username,pwd) elif num == ‘2‘: username = input(‘请输入账号‘) pwd = input(‘请输入密码‘) fucn_list[num](username,pwd) elif num ==‘3‘: fucn_list[num]() elif num ==‘4‘: fucn_list[num]() GOODS_LIST.clear() elif num ==‘5‘: fucn_list[num](SUSS_USER)
以上是关于python小项目练习的主要内容,如果未能解决你的问题,请参考以下文章