ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python —— 小小商城 (用户注册、登陆、浏览购买商品...)

2019-09-14 16:51:04  阅读:136  来源: 互联网

标签:... 用户注册 python file print path line txt os


#!/usr/bin/env pyhton
# -*- conding:utf-8 -*-

# 文件夹自己建
import os import sys import time from datetime import datetime CUN_NAME = None CARS = {} dong = '冻结账户的文件.txt' def user_login(func): def inner(): if not CUN_NAME: print('*************请先登录*************') time.sleep(2) return func() return inner # 用户注册 def register(): print('********用户注册**********') while True: flag = True get_name = input('请输入要注册的用户名(n/N:退出):') if get_name.upper() == 'N': sys.exit() get_pwd = input('请输入密码:') get_date = datetime.now().strftime('%Y-%m-%d') # 获得注册日期 if not os.path.exists('用户信息.txt'): open('用户信息.txt', mode='w') with open('用户信息.txt', 'a+') as files: for line in files.readlines(): a, b, c = line.strip().split('|') if get_name == a: print('用户名已存在,请重新输入!') flag = False if not flag: continue with open('用户信息.txt', mode='a', encoding='utf-8')as file: msg = '%s|%s|%s\n' % (get_name, get_pwd, get_date) file.write(msg) def is_sucss(username, passwd): flag = False user_exist = False if not os.path.exists('用户信息.txt'): open('用户信息.txt', mode='w', encoding='utf-8') with open('用户信息.txt', mode='r', encoding='utf-8')as file: for userinfo in file.readlines(): a, b, c = userinfo.strip().split('|') if a == username: user_exist = True if a == username and b == passwd: print('登陆成功') global CUN_NAME flag = True CUN_NAME = username return user_exist, flag # 检查是否冻结 def freeze(username): if not os.path.exists(dong): return False with open(dong, 'r', encoding='utf-8') as file_Dong: for line in file_Dong.readlines(): if username == line.strip(): print('用户已经被冻结,无法登录,请另外注册!') return True # 用户登陆 def login(): print('****用户登陆****') count = 0 while True: user = input('请输入登录用户名:') pwd = input('请输入登录密码:') with open('用户信息.txt', 'r', encoding='utf-8') as file_data: for line in file_data.readlines(): line = line.strip().split('|') if user == line[0] and pwd == line[1]: print('登录成功') return elif user == line[0]: print('用户密码错误,请重新输入') count += 1 if count == 3: with open(dong, 'a', encoding='utf-8') as file2: file2.write(user + '\n') print('用户被冻结') return print('用户不存在,请注册后登录') return # 分页浏览商品 @user_login def shopping(): while True: li = [] with open('goods.txt', mode='r', encoding='utf-8')as pag: ff = pag.readline() for n in pag.readlines(): li.append(n) meiye = 5 a, b = divmod(len(li), meiye) page = input('请选择页码,最多可选%s页' % (a + 1)) if page.isdecimal() and int(page) <= (a + 1): start = (int(page) - 1) * meiye end = start + meiye else: print('输入页码有误') continue while True: count = 0 goods_cars = {} print('##############商品列表##############') with open('goods.txt', mode='r', encoding='utf-8')as goods_file: for goods_list in goods_file.readlines(): count += 1 if count > end: break if count >= start: a, b, c = goods_list.strip().split('|') goods_cars[a] = {'title': b, 'price': c} for k, v in goods_cars.items(): print(k, v['title'], v['price']) choose = input(r'选择你要得商品(N/n)重新选页(Y/y)结束购买') if choose.upper() == 'N': break if choose.upper() == 'Y': path_all = os.path.join('shopping_car', CUN_NAME) if not os.path.exists(path_all): os.makedirs(path_all) ctime = datetime.now().strftime('%Y-%m-%d_%H_%M_%S') file_path = os.path.join(path_all, ctime) print(file_path) for k, v in CARS.items(): line = '%s|%s|%s|%s\n' % (k, v['title'], v['price'], v['count']) with open(file_path, mode='a', encoding='utf-8')as ff: ff.write(line) return num = input('输入需要购买的数量') if choose in CARS: print('商品已在购物车') CARS[choose]['count'] += int(num) print(CARS) continue CARS[choose] = {'title': goods_cars[choose]['title'], 'price': goods_cars[choose]['price'], 'count': int(num)} print(CARS) @user_login def cars(): path = os.path.join('购物车', CUN_NAME) if not os.path.exists(path): print('此账号没购物') return path_list = os.listdir(path) for n in path_list: file_path = os.path.join(path, n) with open(file_path, mode='r', encoding='utf-8')as file: print(n) for x in file.readlines(): print('\t\t' + x.strip()) time.sleep(2) # 用户选择 def main_page(): while True: print('******欢迎来到韦礼维的小店******\n1.用户注册\n2.用户登陆\n3.查看购物车\n4.浏览商品') info = {1: login, 2: register, 3: cars, 4: shopping} choices = input('请选择序号(输入N/n退出):') func = info.get(int(choices)) if choices == 'N' or 'n': break if not func: print('序号选择错误,请重新选择:') continue func() if __name__ == '__main__': main_page()

 

#  goods.txt  #  商品自行添加 #

'''

商品|价格

1.大炮|2000
2.飞机|1000
3.大炮|2000
4.迫击炮|100000
5.手枪|123
......

'''

标签:...,用户注册,python,file,print,path,line,txt,os
来源: https://www.cnblogs.com/weiliwei-lucky/p/11519519.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有