#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/27 0027 14:07
# @Author : Anthony.Waa
# @Site :
# @File : User_Login_Register_Shopping 1.0.py
# @Software: PyCharm # 购物车
def shopping():
'''
:return:返回商品清单
''' # 原始购物清单
goods = [
{"name": "红旗", "price": 1999},
{"name": "巴厘岛", "price": 10},
{"name": "模特", "price": 20},
{"name": "迪拜", "price": 998},
] # 购物车
shopping_car = {}
# 购物车遍历
shopping_car_list = [] # 商品个数
good_count = 1 # 商品单价
shopping_price = {} # 输入不合法是打印
def return_error():
print("\033[1;31m 输入不合法,请重新输入 \033[0m") # 打印购物车商品和剩余金额
def end_shopping():
# 获取原始列表中的名称和价格
for list_goods_price in goods:
shopping_price[list_goods_price['name']] = list_goods_price['price']
with open('shopping_list.txt', 'w', encoding='utf-8') as shoppingf:
# 获取购物车中的名称和个数,并从中找到与原始列表中名称所对应的价格
for list_shopping_name, list_shopping_count in shopping_car.items():
shoppingf.writelines('商品名称:{0},商品个数:{1}个,商品价格:{2}元\n'.format(list_shopping_name, list_shopping_count,
shopping_price[list_shopping_name])) for list_name in shopping_car:
print('\033[1;32m 商品名称:{0} 商品个数:{1} \033[0m'.format(list_name, shopping_car[list_name]))
print("\033[1;32m 剩余金额为:{0}\033[0m".format(all_money)) while True:
# 用户输入总资产
print('\033[1;32m=\033[0m' * 50, '\n')
all_money = input("\033[1;32m 请输入总资产: \033[0m").strip()
if all_money.isdigit():
all_money = int(all_money)
while True:
# 显示购物商品列表
print('\033[1;32m=\033[0m' * 50, '\n')
print(' 序号: 商品名: 单价:')
for good_index, good in enumerate(goods, 1):
print('\033[1;32m {0} {1} {2} \033[0m'.format(good_index, good['name'],good['price']).center(20))
print('\033[1;32m 选择"q"退出 \033[0m'.center(20))
print('\033[1;32m=\033[0m' * 50, '\n') # 选择商品序号,或选择退出购物
choice_count = input("\033[1;32m 请输入商品序号: \033[0m").strip()
if choice_count.isdigit():
choice_count = int(choice_count)
# 商品序号小于商品列表长度
if choice_count > 0 and choice_count <= len(goods):
good_price = goods[choice_count - 1]['price']
# 用户资产大于商品价格,否则提示充值
if all_money > good_price:
good_name = goods[choice_count - 1]['name']
all_money -= good_price
# 商品是否存在购物车中
if good_name not in shopping_car:
shopping_car[good_name] = good_count
else:
for shopping_index, shopping_name in enumerate(shopping_car):
if shopping_name == good_name:
shopping_car[shopping_name] += 1 end_shopping() else:
print('\033[1;31m 余额不足,请充值: \033[0m'.center(17))
add_money = input("请输入总资产:").strip()
if add_money.isdigit():
add_money = int(add_money)
all_money += add_money
print('\033[1;32m 充值成功,剩余金额为: \033[0m'.center(17), all_money)
continue
elif choice_count.lower() == 'q':
print("\033[1;32m 购物结束,欢迎下次光临 \033[0m")
end_shopping()
return False
return_error() def user_login():
count = 0
while True:
print('-' * 55)
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
with open('register.txt', 'r', encoding='utf-8') as f1:
for line in f1:
line_list = line.strip().replace(',', ',').split(',')
if username == line_list[0] and password == line_list[1]:
print('登陆成功')
status_dict['username'] = username
status_dict['status'] = True
return True
else:
print('输入错误')
count += 1
if count == 3:
print('用户名或密码输入错误超过3次,已锁定.')
exit() def user_regiter(*args, **kwargs):
while True:
print('-' * 55)
print('提示:用户名任意填写,不能超过8位,密码为数字,字母,下划线,不能超过16位')
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if len(username) > 8 and len(password) > 16:
print('用户名或密码长度不符合规定,请重新输入...')
continue
else:
with open('register.txt', 'r', encoding='utf-8') as f1:
for line in f1:
line_list = line.strip().replace(',', ',').split(',')
if username == line_list[0] and password == line_list[1]:
print('该用户已存在,请重新输入')
break
else:
with open('register.txt', 'a+', encoding='utf-8') as f2:
f2.writelines('{0},{1}{2}'.format(username, password, '\n'))
print('注册成功..')
print('自动登录中...')
status_dict['username'] = username
status_dict['status'] = True
print('登录成功,请选择...')
return True def article():
print('-' * 55)
print('欢迎进入文章视图') def notebook():
print('-' * 55)
print('欢迎进入日记视图') def comment():
print('-' * 55)
print('欢迎进入评论视图') def collection():
print('-' * 55)
print('欢迎进入收藏视图') def cancellation():
print('-' * 55)
print('用户已注销')
status_dict['status'] = False
return False def sign_out():
print('-' * 55)
print('程序已退出')
exit() choice_menus = {
1: user_regiter,
2: user_login,
3: article,
4: notebook,
5: comment,
6: collection,
7: shopping,
8: cancellation,
9: sign_out,
}
print('-' * 55) show_menu = {
1: '用户注册',
2: '用户登录',
3: '文章页面',
4: '日记页面',
5: '评论页面',
6: '收藏页面',
7: '商城',
8: '注销',
9: '退出程序',
} # 全局用户登录状态
status_dict = {
'username': None,
'status': False,
} import time # 用户访问内容
visit_website = [] # 打印用户选项日志
def outside(log):
def into_log(*args, **kwargs):
with open('loging.log', 'a+', encoding='utf-8') as log1:
start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log(*args, **kwargs)
stop_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log1.writelines(
'\n用户:{0},登录时间:{1},退出时间:{2},使用函数:{3},欢迎{4}用户,访问{5}'.format(status_dict['username'], start_time,stop_time, choice_menus[choice_menu],status_dict['username'], visit_website[-1])) return into_log # 运行主体
while True:
# 打印主菜单
print(' 欢迎登陆博客园')
for menu_index, menu in enumerate(show_menu, 1):
print(' {0}、{1}'.format(menu_index, show_menu[menu]))
print('-' * 55) choice_menu = input('请输入以上任一选项:').strip()
if choice_menu.isdigit():
choice_menu = int(choice_menu)
# 添加用户的访问内容至visit_website列表记录
if choice_menus[choice_menu] not in visit_website:
visit_website.append(show_menu[choice_menu]) #装饰选项开始
@outside
def print_main():
if choice_menu == 9:
choice_menus[9]()
else:
if 0 < choice_menu <= len(choice_menus):
# 判断用户若没有登录,只能输入选项为1、2
if status_dict['status'] == False:
print("\033[1;32m提示:请登录后再尝试其他选项...\033[0m")
if 0 < choice_menu <= 2:
choice_menus[choice_menu]()
# 登录成功后,选择其他选项
else:
choice_menus[choice_menu]()
else:
print('选项不存在,请重新输入...') print_main()
else:
print('输入错误,请重新输入...')

User_Login_Register_Shopping+装饰器 3.0的更多相关文章

  1. python cookbook第三版学习笔记二十:可自定义属性的装饰器

    在开始本节之前,首先介绍下偏函数partial.首先借助help来看下partial的定义 首先来说下第一行解释的意思: partial 一共有三个部分: (1)第一部分也就是第一个参数,是一个函数, ...

  2. s14 第4天 关于python3.0编码 函数式编程 装饰器 列表生成式 生成器 内置方法

    python3 编码默认为unicode,unicode和utf-8都是默认支持中文的. 如果要python3的编码改为utf-8,则或者在一开始就声明全局使用utf-8 #_*_coding:utf ...

  3. 【Flask】 python学习第一章 - 4.0 钩子函数和装饰器路由实现 session-cookie 请求上下文

    钩子函数和装饰器路由实现 before_request 每次请求都会触发 before_first_requrest  第一次请求前触发 after_request  请求后触发 并返回参数 tear ...

  4. es6 装饰器decorator的使用 +webpack4.0配置

    decorator 装饰器 许多面向对象都有decorator(装饰器)函数,比如python中也可以用decorator函数来强化代码,decorator相当于一个高阶函数,接收一个函数,返回一个被 ...

  5. Python高手之路【四】python函数装饰器

    def outer(func): def inner(): print('hello') print('hello') print('hello') r = func() print('end') p ...

  6. Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

    本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ...

  7. python --> 递归 以及装饰器

    一.递归知识 函数迭套执行,逐层执行之后,满足某个条件之后就会停止执行,将return值返回上层的函数,上层函数再逐层返回,最终返回给最初始函数. 递归在斐波那契数列的应用[斐波那契数列特点:前两个数 ...

  8. python学习笔记(5)--迭代器,生成器,装饰器,常用模块,序列化

    生成器 在Python中,一边循环一边计算的机制,称为生成器:generator. 如: >>> g = (x * x for xin range(10)) >>> ...

  9. python 函数之装饰器,迭代器,生成器

    装饰器 了解一点:写代码要遵循开发封闭原则,虽然这个原则是面向对象开发,但也适用于函数式编程,简单的来说,就是已经实现的功能代码不允许被修改但 可以被扩展即: 封闭:已实现功能的代码块 开发:对扩张开 ...

随机推荐

  1. Android--XML页面的编写

           五个页面  代码如下:    图片资源链接: https://pan.baidu.com/s/1jIoTDGE //  第一个 <RelativeLayout xmlns:andr ...

  2. 搭建eclipse的安卓开发环境(eclipse+jdk+adt+sdk)

    学校暑期大作业让用安卓写一个app,有两种方案(android stduio+sdk和eclipse+jdk+adt+sdk)折腾了几天发现还是后者好用,但是安装环境和下载真的是去了半条命,(不过由于 ...

  3. UBuntu安裝使用PIP

    Windows下安裝python包還是比較方便的,直接在FLD網站下載對應的EXE文件就可以安裝,在linux系統下,使用pip,easy egg 管理工具可以減輕安裝負擔. 原文鏈接:http:// ...

  4. SQL的类型转换

    说到SQL类型转换,我们知道SQL是一个弱类型语言,所以可以做隐式的强制类型转换,下面记录一下这几天遇到的类型转换问题. 1.在SQL中,字符串类型与数字之间转换是不需要强制类型转换符的,如果字符串是 ...

  5. PHP 判断一个字符是否在字符串中

    strpos() - 查找字符串 在另一字符串中 第一次出现的位置(区分大小写) stripos() - 查找字符串 在另一字符串中 第一次出现的位置(不区分大小写) strrpos() - 查找字符 ...

  6. HDU 5901 Count primes( Meisell-Lehmer算法模板 )

    链接:传送门 题意:计算 [ 1 , n ] 之间素数的个数,(1 <= n <= 1e11) 思路:Meisell-Lehmer算法是计算超大范围内素数个数的一种算法,原理并不明白,由于 ...

  7. 实现路由器自动登录校园网(edu)

    准备工作: (1)一个可以刷openwrt固件的路由器,如大多人使用的crazybox版本的路由. (2)一个可用的edu账号. (3)一个浏览器(firfox,chrome) 下面开始: 一:刷op ...

  8. crontab 设置定时任务

    查看当前用户已有的定时任务: crontab -l 编辑crontab: crontab -e 加入需要执行的命令: 0 */4 * * * /www/shwww.net/venv/bin/pytho ...

  9. java的值传递与引用传递

    一. 经常搞不清楚,当一个对象做为参数传入到方法中时,为啥有时候值能被改变,有时候又不会改变,以下说明原因: 1.当传入的参数,在方法中能被改变的为 引用传递 2.当传入的参数,在方法中没被改变的为 ...

  10. 转-----------------------js window.open() 操作

    <% if request("infoid")<>"" then set rs=conn.execute("select * fro ...