python实现简单的购物车
import json,time
userinfo={"lanfei":
{ "passwd":"lanfei",
"salary":3000,
"buy_commodity":{ "time":None,
"commodity_name":None,
"price":None,},
"shopping_cart":{
"commodity_name":None,
"price":None,}
}
}
commodity_list={"ipad":{"price":100,"orign_place":"北京"}}
def import_to_file():
try:
f1=open(r"F:\userinfo.txt",'w',encoding="utf-8")
f2=open(r"F:\commodity.txt",'w',encoding="utf-8")
json.dump(userinfo,f1)
json.dump(commodity_list,f2)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def export_from_file():
global userinfo
global commodity_list
global commodity_list
try:
f1=open(r"F:\userinfo.txt",'r',encoding="utf-8")
f2=open(r"F:\commodity.txt","r",encoding="utf-8")
userinfo=json.load(f1)
commodity_list=json.load(f2)
f1.close()
f2.close
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def creat_user(name,passwd):
export_from_file()
if name in userinfo.keys():
print("usernam has exist!")
name=input("rewrite your username:")
passswd=input("rewrite your userpassswd:")
creat_user(name,passwd)
else:
userinfo.setdefault(name,{"passwd":passwd,"salary":None})
return True
def add_commmdity(mount):
print("商品添加".center(30))
print('-'*35)
for i in range(mount):
commodity=input("输入要添加的商品:")
price=eval(input("输入商品的价格:"))
info=input("输入商品的信息:")
commodity_list.setdefault(commodity,{"price":price,"orign_place":None})
if commodity not in commodity_list.keys():
print("%s添加失败"%commodity)
else:
print("全部添加成功")
import_to_file() def authentication(name,passwd):
export_from_file()
if name in userinfo.keys():
if passwd==userinfo[name]["passwd"]:
return True
else:
return False
else:
return False
def shopping(name):
while True:
print("商品列表".center(30))
print("-" * 35)
for index,item in commodity_list.items():
print("%s:\n\t%s\n\t%s"%(index,item["price"],item["orign_place"]))
commodity_name=input("输入您想要购买的商品名称:")
while commodity_name not in commodity_list.keys():
commodity_name=input("商品不存在,请重新输入:")
price=commodity_list[commodity_name]["price"]
if userinfo[name]["salary"]>=price:
ack=input("确认购买(y/n):")
if ack=="y":
userinfo[name]["salary"]-=price
userinfo[name].setdefault("buy_commodity",{"time":time.ctime(),"price":price,"commdity_name":commodity_name})
print("购买成功,您的余额:%s"%userinfo[name]["salary"])
elif ack=='no':
continue
elif ack=='exit':
break
else:
print("余额不足,请充值!")
ack=input("是否退出程序(y/n):")
if ack=="y":
exit()
else:
continue
def user_login_interface():
function=input("登录输入login,注册输入regist:")
if function !="login" and function != "regist":
print("输入错误!")
name=input("username:")
passwd=input("userpassswd:")
if function=="login":
if authentication(name,passwd)==True:
print("login successfully!")
shopping(name)
else:
print("username or userpasswd error!")
elif function=="regist":
if creat_user(name,passwd)==True:
import_to_file()
print("regist sucessfully!")
def merchant_login_interface():
function=input("登录输入login,注册输入regist:")
if function !="login" and function != "regist":
print("输入错误!")
exit()
name=input("username:")
passwd=input("userpassswd:")
if function=="login":
if authentication(name,passwd)==True:
print("login successfully!")
mount=eval(input("输入添加商品的数目:"))
add_commmdity(mount)
else:
print("username or userpasswd error!")
elif function=="regist":
if creat_user(name,passwd)==True:
import_to_file()
print("regist sucessfully!")
#user_login_interface()
merchant_login_interface()
python实现简单的购物车的更多相关文章
- python实现简单的循环购物车小功能
python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...
- 第一个简单的python程序,模拟购物车
第一个pyhone程序,简单的购物车功能,刚开始学习,练习下手感. #/usr/bin/env python#-*- coding:utf-8 -*- salary = int(raw_input(& ...
- Python3学习之路~2.2 简单的购物车程序
程序:购物车程序 需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 代码 ...
- 使用MongoDB和JSP实现一个简单的购物车系统
目录 1 问题描述 2 解决方案 2.1 实现功能 2.2 最终运行效果图 2.3 系统功能框架示意图 2.4 有关MongoDB简介及系统环境配置 2.5 核心功能代码讲解 ...
- Python 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...
- python开启简单webserver
python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...
- FineUI小技巧(1)简单的购物车页面
起因 最初是一位 FineUI 网友对购物车功能的需求,需要根据产品单价和数量来计算所有选中商品的总价. 这个逻辑最好在前台使用JavaScript实现,如果把这个逻辑移动到后台C#实现,则会导致过多 ...
- Python开发简单爬虫 - 慕课网
课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序 课程目录 第1章 课程介绍 ...
随机推荐
- 时间插件--daterangepicker使用和配置详解
1.序言: daterangepicker是Bootstrap的一个时间组件,使用很方便 用于选择日期范围的JavaScript组件. 设计用于Bootstrap CSS框架. 它最初是为了改善报表而 ...
- centos django Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
os环境 centos python2.7.5 django1.10.8 class AdminAutoRunTask(View): """ 自动跑外放任务 " ...
- bis和bic命令实现或和异或运算
从20世纪70年代末到80年代末,Digital Equipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,只有bis(位设置)和bic(位清除)这两种指令.两种指令的输入 ...
- python控制台输出带颜色的文字方法
#格式: 设置颜色开始 :\033[显示方式;前景色;背景色m 注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于表示三个参数不同含义的数值都是唯一的没有 ...
- Gaussian Process for Regression
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...
- 海亮OI学习游记
这只是一篇纯洁的游记,这里将要记录我在海亮十天集训的生活与被虐的历史QWQ...... Day1(2.10)刚来到海亮,嗯,这的环境真的不错. 来到机房,woc这机房的配置好高啊...这里都能打守望屁 ...
- CMDB服务器管理系统【s5day91】:数据库表结构补充
1.表机构补充图 2.用户信息表(UserProfile) 1.解决了什么问题 1.这台服务器是谁管理的?2.真正出问题了我敢上去改代码了?不能3.所以一台机器必须有运维人员和业务负责人,但是业务负责 ...
- Java基础知识拾遗(一)
类型提升规则 Java定义了几个应用于表达式的类型提升规则:所有byte.short和char类型的值都被提升为int类型.如果有一个操作数是long类型,将这个表达式提升为 long 类型:如果有一 ...
- Client-Side Template Injection with AngularJS
<html> <head> <meta charset="utf-8"> <script src="https://cdn.bo ...
- 第二十三节: EF性能篇(三)之基于开源组件 Z.EntityFrameWork.Plus.EF6解决EF性能问题
一. 开篇说明 EF的性能问题一直以来经常被人所吐槽,究其原因在于“复杂的操作在生成SQL阶段耗时长,且执行效率不高”,但并不是没有办法解决,从EF本身举几个简单的优化例子: ①:如果仅是查询数据,并 ...