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实现简单的购物车的更多相关文章

  1. python实现简单的循环购物车小功能

    python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...

  2. 第一个简单的python程序,模拟购物车

    第一个pyhone程序,简单的购物车功能,刚开始学习,练习下手感. #/usr/bin/env python#-*- coding:utf-8 -*- salary = int(raw_input(& ...

  3. Python3学习之路~2.2 简单的购物车程序

    程序:购物车程序 需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 代码 ...

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

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

  5. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  6. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  7. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  8. FineUI小技巧(1)简单的购物车页面

    起因 最初是一位 FineUI 网友对购物车功能的需求,需要根据产品单价和数量来计算所有选中商品的总价. 这个逻辑最好在前台使用JavaScript实现,如果把这个逻辑移动到后台C#实现,则会导致过多 ...

  9. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

随机推荐

  1. Python——迭代器

    一.概述 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 二.可迭代的对象 序列:字符串.列表.元组 非序列:字典.文件 三 ...

  2. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  3. Zabbix配置邮件监控

    zabbix服务端配置 安装软件并配置 使用第三方邮件实现报警 1. 安装软件 $ yum -y install mailx 2. 配置发送邮件账号密码和服务器 $ vim /etc/mail.rc ...

  4. [UOJ422][集训队作业2018]小Z的礼物——轮廓线DP+min-max容斥

    题目链接: [集训队作业2018]小Z的礼物 题目要求的就是最后一个喜欢的物品的期望得到时间. 根据$min-max$容斥可以知道$E(max(S))=\sum\limits_{T\subseteq ...

  5. 动态库 Framework

    framework的建立和生成 都比较简单.重点会放在第三块上面(指令集说明及合并) 1.framework target建立 1.1. command + shift + N 选取 ios -> ...

  6. EM算法(Expectation Maximization Algorithm)初探

    1. 通过一个简单的例子直观上理解EM的核心思想 0x1: 问题背景 假设现在有两枚硬币Coin_a和Coin_b,随机抛掷后正面朝上/反面朝上的概率分别是 Coin_a:P1:-P1 Coin_b: ...

  7. SpringBoot系列: 使用 consul 作为服务注册组件

    本文基本上摘自纯洁的微笑的博客 http://www.ityouknow.com/springcloud/2018/07/20/spring-cloud-consul.html . 感谢作者的付出. ...

  8. Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception

    1.HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 解决 使用jre1.7 Spring4.3 2.spr ...

  9. opensuse 使用xx-net

    提示安装 [launcher][WARNING] import pynotify fail, please install python-notify if possible. gae_proxy][ ...

  10. IDEA远程调试服务器代码

    先在idea添加一个remote,host填服务器ip,port填监听服务器端口,默认5005 然后在服务器tomcat catalina.sh 添加(红色部分): JAVA_OPTS="$ ...