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. ABP拦截器之UnitOfWorkRegistrar(一)

    ABP中UnitOfWorkRegistrar拦截器是整个ABP中非常关键的一个部分,这个部分在整个业务系统中也是用的最多的一个部分,这篇文章的主要思路并不是写如何使用ABP中的UnitOfWork, ...

  2. Java垃圾收集器概述

    垃圾收集器的操作 查找未使用的对象,释放内存,并压缩堆,避免内存碎片 一个java程序,有执行应用程序逻辑的线程和执行GC的线程组.当GC跟踪对象引用,或在内存中移动对象,它必须确保应用程序线程没有使 ...

  3. Python 字符编码及其文件操作

    本章节内容导航: 1.字符编码:人识别的语言与机器机器识别的语言转化的媒介. 2.字符与字节:字符占多少个字节,字符串转化 3.文件操作:操作硬盘中的一块区域:读写操作 注:浅拷贝与深拷贝 用法: d ...

  4. PHP——判断是否为加密协议https

    前言 就是一个封装的方法,用来判断域名前面是加http还是https 代码 function is_ssl() { if(isset($_SERVER['HTTPS']) && ('1 ...

  5. Apache服务器配置与管理

    一.Apache服务器的目录和文件 1.WEB站点目录 /var/www Apache站点文件的目录 /var/www/html 存放WEB站点的WEB文件 /var/www/cgi-bin CGI程 ...

  6. jq动画实现左右滑动

    <!DOCTYPE html> <html> <head> <title>jquery动画滑动</title> <style type ...

  7. Docker:dockerfile镜像的分层 [九]

    一.docker镜像的分层 1.图像呈现 2.命令呈现 [root@oldboy kod]# docker image history kod:v1 IMAGE CREATED CREATED BY ...

  8. SpringBoot项目@RestController使用 redirect 重定向无效

    Spring MVC项目中页面重定向一般使用return "redirect:/other/controller/";即可. 而Spring Boot当我们使用了@RestCont ...

  9. Cygwin添加到鼠标右键

    From:http://www.cnblogs.com/killerlegend/p/3960112.html 在cygwin中安装chere 管理员权限执行mintty,输入下列命令即可. cher ...

  10. 第二节:比较DateTime和DateTimeOffset两种时间类型并介绍Quartz.Net中用到的几类时间形式(定点、四舍五入、倍数、递增)

    一. 时间的类型 1. 背景 这里为什么要介绍时间类型呢,明明是定时调度篇,原因是在定时任务中,任务什么时间开始执行,什么时间结束执行,要用到各种各样的时间模式,虽然这不能算是一个复杂的问题,但在正式 ...