python —— 小小商城 (用户注册、登陆、浏览购买商品...)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python —— 小小商城 (用户注册、登陆、浏览购买商品...),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4393字,纯文字阅读大概需要7分钟。
内容图文
![python —— 小小商城 (用户注册、登陆、浏览购买商品...)](/upload/InfoBanner/zyjiaocheng/1297/058b8ab21e124babacdf7fcbfdf66f68.jpg)
# !/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(): ifnot 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‘) # 获得注册日期ifnot 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 ifnot 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 ifnot 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): ifnot 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(‘登录成功‘) returnelif user == line[0]: print(‘用户密码错误,请重新输入‘) count += 1 if count == 3: with open(dong, ‘a‘, encoding=‘utf-8‘) as file2: file2.write(user + ‘\n‘) print(‘用户被冻结‘) returnprint(‘用户不存在,请注册后登录‘) 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(‘输入页码有误‘) continuewhile 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: breakif 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‘: breakif choose.upper() == ‘Y‘: path_all = os.path.join(‘shopping_car‘, CUN_NAME) ifnot 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) ifnot 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‘: breakifnot func: print(‘序号选择错误,请重新选择:‘) continue func() if__name__ == ‘__main__‘: main_page()
# goods.txt # 商品自行添加 #
‘‘‘
商品|价格
1.大炮|2000
2.飞机|1000
3.大炮|2000
4.迫击炮|100000
5.手枪|123
......
‘‘‘
原文:https://www.cnblogs.com/weiliwei-lucky/p/11519519.html
内容总结
以上是互联网集市为您收集整理的python —— 小小商城 (用户注册、登陆、浏览购买商品...)全部内容,希望文章能够帮你解决python —— 小小商城 (用户注册、登陆、浏览购买商品...)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】