Python-购物车系统
# coding=utf-8
import os, pickle
class color:
def echo_error(self, red):
print(f"\033[31;1m {red}\033[0m".strip())
def echo_ok(self, green):
print(f"\033[32;1m {green}\033[0m".strip())
def echo_warn(self, warning):
print(f"\033[33;1m {warning}\033[0m".strip())
color = color()
def file_open(file, mode, *args):
if mode == "wb":
data = args[0]
with open(file, mode) as f:
pickle.dump(data, f)
elif mode == "rb":
with open(file, mode) as f:
user_dict = pickle.load(f)
return user_dict
elif mode == "clear":
with open(file, mode) as f:
data = args[0]
pickle.dump(data, f)
def user(user, pwd, mode):
file = user + ".db"
if mode == "regist":
if not os.path.isfile(file):
user_dict = {"name": user, "pwd": pwd, "stat": 0}
file_open(file, "wb", user_dict)
color.echo_ok("注册成功!")
else:
color.echo_error("用户已注册")
elif mode == "login":
if os.path.isfile(file):
user_dict = file_open(file, "rb")
if user_dict["name"] == user and user_dict["pwd"] == pwd:
color.echo_ok("登录成功")
return user_dict
else:
color.echo_error("登录失败,用户名或密码错误")
else:
color.echo_error("不存在此用户,请先注册")
return None
else:
color.echo_error("错误")
def shopping(user_dict, list_goods):
if user_dict["shopping_car"]:
list_shopping = user_dict["shopping_car"]
else:
list_shopping = []
while True:
color.echo_ok("商品列表如下:\n"
"##########################\n"
"序号 商品名称 商品价格")
for i, k in enumerate(list_goods):
print(i, k["name"], k["price"])
chooice = input("请输入购买商品序号 [t 查看购物车 q 退出]")
if not chooice: continue
if chooice.isdigit() and 0 <= int(chooice) < len(list_goods):
num = input("请输入要购买的数量")
if num.isdigit():
num = int(num)
else:
color.echo_error("数量必须是正整数")
goods = {"name": list_goods[int(chooice)]["name"], "num": num,
"total_price": list_goods[int(chooice)]["price"] * num}
list_shopping.append(goods)
color.echo_ok("购物成功!")
elif chooice == "t":
color.echo_ok("#########购物清单##########")
print("商品名 数量 价格")
if len(list_shopping) == 0:
color.echo_warn("您没有购买任何东西..")
any = input("输入任意字符继续..")
if any: continue
continue
price = 0
for i in list_shopping:
print(i["name"], i["num"], i["total_price"])
price += i["total_price"]
chooice = input("总价格:%s,是否购买(yes/no),[e 查看余额 c 清空购物车]:" % price)
if chooice == "c":
list_shopping = []
user_dict["shopping_car"] = list_shopping
color.echo_ok("购物车清空成功")
file_open(user_dict["name"] + ".db", "wb", user_dict, "clear")
elif chooice == "e":
color.echo_ok("您的余额为 %s" % user_dict["money"])
elif chooice == "yes":
if user_dict["money"] > price:
res_money = user_dict["money"] - price
color.echo_ok(f"购买成功,您的余额为 {res_money}")
list_shopping = []
user_dict["shopping_car"] = list_shopping
user_dict["money"] = res_money
file_open(user_dict["name"] + ".db", "wb", user_dict, "clear")
any = input("输入任意字符继续..")
if any: continue
else:
color.echo_error("您的余额不够买这些东西,挣点钱再来吧。")
elif chooice == "no":
color.echo_warn("您取消购买")
any = input("输入任意字符继续..")
if any: continue
elif chooice == "q":
color.echo_warn("退出程序")
break
def start(list_goods):
while True:
print('''
1. 注册
2. 登录并购物
3. 登出
''')
chooce = input(">>>").strip()
if chooce == "1":
username = input("请输入你的用户名:").strip()
pwd = input("请输入你的密码:").strip()
user(username, pwd, "regist")
elif chooce == "2":
username = input("请输入你的用户名:").strip()
pwd = input("请输入你的密码:").strip()
user_dict = user(username, pwd, "login")
if user_dict == None: continue
if user_dict["stat"] == 0:
money = int(input("请输入你的首冲金额:"))
user_dict["stat"] += 1
user_dict["money"] = money
user_dict["shopping_car"] = []
color.echo_ok("恭喜,充值成功!,可以尽情购物了~")
file_open(user_dict["name"] + ".db", "wb", user_dict)
shopping(user_dict, list_goods)
else:
shopping(user_dict, list_goods)
elif chooce == "3":
break
else:
color.echo_error("非法操作,请重输入")
if __name__ == "__main__":
list_goods = [
{"name": "苹果", "price": 10},
{"name": "芒果", "price": 20},
{"name": "提子", "price": 30},
{"name": "香蕉", "price": 40},
]
start(list_goods)
Python-购物车系统的更多相关文章
- python购物车系统
购物车系统模拟:product_list = [ ('java',100), ('python',200), ('键盘',500), ('电脑',4000), ('mac Book',7000),]S ...
- python实现简单购物车系统(练习)
#!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('comput ...
- ATM系统和购物车系统 不需要文件支撑
目录 ATM系统 购物车系统 ATM系统 #coding=utf8 #Version:python 3.6.4 #Tools:Python 2019.9.7 _data_ = '2019/9/7/01 ...
- 使用MongoDB和JSP实现一个简单的购物车系统
目录 1 问题描述 2 解决方案 2.1 实现功能 2.2 最终运行效果图 2.3 系统功能框架示意图 2.4 有关MongoDB简介及系统环境配置 2.5 核心功能代码讲解 ...
- Java课设-购物车系统
1.团队课程设计博客链接 /[博客链接]http://www.cnblogs.com/yayaya/p/7062197.html 2.个人负责模板或任务说明 1.建立Action类 2.购物车的属性 ...
- Java课程设计 购物车系统(个人博客)
1. 团队课程设计博客链接 课程设计 2. 个人负责模块或任务说明 编写ShoppingCart类,连接数据库 编写updateCart类,从数据库中获取商品信息,获取指定编号的商品信息 编写User ...
- java 课程设计 购物车系统 个人
Q1.团队课程设计博客链接 团队博客 Q2.个人负责模块或任务说明 我主要负责main函数的编写和系统中瞎看功能代码的编写. Q3.自己的代码提交记录截图 main函数代码如下: public sta ...
- Python云端系统开发入门——框架基础
Django框架基础 这是我学习北京理工大学嵩天老师的<Python云端系统开发入门>课程的笔记,在此我特别感谢老师的精彩讲解和对我的引导. 1.Django简介与安装 Django是一个 ...
- python 保障系统(一)
python 保障系统 from django.shortcuts import render,redirect,HttpResponse from app01 import models from ...
- python 报障系统(完)
python 报障系统(完) 一.报障系统原理: 原理: 1. 简单管理 2. 角色多管理(权限) a. 登录 session放置用户信息(检测是否已经登录) session放置权限信息(检测是否有权 ...
随机推荐
- 阿里云宣布 Serverless 容器服务 弹性容器实例 ECI 正式商业化
摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化,ECI 是阿里云践行普惠的云计算理念,将 Serverless 和 Container 技术结 ...
- lxhgww的奇思妙想 长链剖分板子
https://vijos.org/d/Bashu_OIers/p/5a79a3e1d3d8a103be7e2b81 求k级祖先,预处理nlogn,查询o1 //#pragma GCC optimiz ...
- CNN 常用的几个模型
LeNet5 论文:http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf LeNet-5:是Yann LeCun在1998年设计的用于手写数字识别的卷 ...
- java中字符数组与字符串之间互相转换的方法
public static void main(String[] args) { //1.字符数组 转换成 字符串 //(1)直接在构造String时转换 char[] array = new cha ...
- 数据类中引用virtual
public class City { [Key] public int CityID { set; get; } [Display(Name = "城市名称")] [Requir ...
- 笔记:使用Python解析JSON
使用Python解析JSON json是一种轻量级的数据交换格式,易于阅读和编写. json函数具体作用描述 函数 具体描述作用 json.dumps 将python对象编码为JSON字符串 json ...
- [Baltic2009]beetle【区间Dp】
Online Judge:Bzoj1761 Label:区间Dp 题目描述 在一条直线上有N个点,每个点M升水. 一个虫子在坐标轴0点上,它每个单位时间移动一格,每个点的水每单位时间消失1升. 问虫子 ...
- 满血复活前的记录(持续更新ing)
时隔一年重新开启算法竞赛征程. 该记录大多为老课件.已经做过的习题重做和已经看过的书本重看 7.21 下午到山大 娄晨耀basic_algorithm课件中的内容: 复习线性筛原理 复习差分 做完Co ...
- C++ 系列:Boost Thread 编程指南
转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...
- Tensorflow入门篇
参考Tensorflow中文网(http://www.tensorfly.cn/tfdoc/get_started/introduction.html) ,写一个入门. 1.打开pyCharm,新建 ...