# 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-购物车系统的更多相关文章

  1. python购物车系统

    购物车系统模拟:product_list = [ ('java',100), ('python',200), ('键盘',500), ('电脑',4000), ('mac Book',7000),]S ...

  2. python实现简单购物车系统(练习)

    #!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('comput ...

  3. ATM系统和购物车系统 不需要文件支撑

    目录 ATM系统 购物车系统 ATM系统 #coding=utf8 #Version:python 3.6.4 #Tools:Python 2019.9.7 _data_ = '2019/9/7/01 ...

  4. 使用MongoDB和JSP实现一个简单的购物车系统

    目录 1 问题描述  2 解决方案  2.1  实现功能  2.2  最终运行效果图  2.3  系统功能框架示意图  2.4  有关MongoDB简介及系统环境配置  2.5  核心功能代码讲解  ...

  5. Java课设-购物车系统

    1.团队课程设计博客链接 /[博客链接]http://www.cnblogs.com/yayaya/p/7062197.html 2.个人负责模板或任务说明 1.建立Action类 2.购物车的属性 ...

  6. Java课程设计 购物车系统(个人博客)

    1. 团队课程设计博客链接 课程设计 2. 个人负责模块或任务说明 编写ShoppingCart类,连接数据库 编写updateCart类,从数据库中获取商品信息,获取指定编号的商品信息 编写User ...

  7. java 课程设计 购物车系统 个人

    Q1.团队课程设计博客链接 团队博客 Q2.个人负责模块或任务说明 我主要负责main函数的编写和系统中瞎看功能代码的编写. Q3.自己的代码提交记录截图 main函数代码如下: public sta ...

  8. Python云端系统开发入门——框架基础

    Django框架基础 这是我学习北京理工大学嵩天老师的<Python云端系统开发入门>课程的笔记,在此我特别感谢老师的精彩讲解和对我的引导. 1.Django简介与安装 Django是一个 ...

  9. python 保障系统(一)

    python  保障系统 from django.shortcuts import render,redirect,HttpResponse from app01 import models from ...

  10. python 报障系统(完)

    python 报障系统(完) 一.报障系统原理: 原理: 1. 简单管理 2. 角色多管理(权限) a. 登录 session放置用户信息(检测是否已经登录) session放置权限信息(检测是否有权 ...

随机推荐

  1. http://wiki.ros.org/navigation/Tutorials/RobotSetup

    http://wiki.ros.org/navigation/Tutorials/RobotSetup

  2. thinkphp一键清除缓存的方法

    后台控制器: <?php namespace Home\Controller; use Think\Controller; class HuancuController extends Cont ...

  3. 【Vue】组件的基础与组件间通信

    转载:https://segmentfault.com/a/1190000016409329 Vue.js 最核心的功能就是组件(Component),从组件的构建.注册到组件间通信,Vue .x 提 ...

  4. 17.splash_case01

    # 抓取今日头条,对比渲染和没有渲染的效果 import requests from lxml import etree # url = 'http://localhost:8050/render.h ...

  5. Geoserver的跨域问题

    使用tomcat访问Geoserver服务的时候,只调服务没问题,但是查询要素属性的时候出现如下“XMLHttpRequest”.“not allowed by Access-Control-Allo ...

  6. SSM三大框架的运行流程、原理、核心技术详解

    一.Spring部分1.Spring的运行流程第一步:加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("be ...

  7. Entity Framework Code First 模式-建立多对多联系

    Entity Framework 在建立多对多的联系时,会生成一个中间表,用来表示这个多对多的关系.这和数据库设计时从概念模型到逻辑模型转化时,多对多的关系不能和任何一端的实体合并,需要将关系也转化为 ...

  8. C++:多线程002

    https://blog.csdn.net/morewindows/article/details/7442333 程序描述:主线程启动10个子线程并将表示子线程序号的变量地址作为参数传递给子线程.子 ...

  9. Powerdesigner 生成数据字典

    https://www.jianshu.com/p/f491d0d3c503http://blog.csdn.net/adparking/article/details/50402980http:// ...

  10. CentOS7服务器中apache、php7以及mysql5.7的安装配置代码

    CentOS7服务器中apache.php7以及mysql5.7的配置代码如下所示: yum upgradeyum install net-tools 安装apache (http://m.86822 ...