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. CodeSmith如何生成实体类

    CodeSmith如何生成实体类 这是模板,然后选择对应的表.就可以生成 了 <%-- Name: Database Table Properties Author: Paul Welter D ...

  2. Python——Django-urls.py的作用

    一.urls.py的作用是保存路径和函数的对应关系 二.函数返回指定内容 from django.urls import path #引用HTTP协议的代码 from django.shortcuts ...

  3. 图论(最短路&最小生成树)

    图论 图的定义与概念 图的分类 图,根据点数和边数可分为三种:完全图,稠密图与稀疏图. 完全图,即\(m=n^2\)的图\((m\)为边数,\(n\)为点数\()\).如: 1 1 0 1 2 1 1 ...

  4. 柳叶刀重磅出击!全外显子测序在胎儿结构异常的评估Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective cohort study

    柳叶刀发表的文献解读:Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective coh ...

  5. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. Go-常用库的介绍

    一.Go常用包介绍  fmt.它实现了格式化的输入输出操作,其中的fmt.Printf()和fmt.Println()是开 发者使用最为频繁的函数.  io.它实现了一系列非平台相关的IO相关接口 ...

  7. 如何巧妙的使用ArrayList的Clone方法

    一.ArrayList的Clone方法的源码 返回一个Object对象,所以在使用此方法的时候要强制转换. ArrayList的本质是维护了一个Object的数组,所以克隆也是通过数组的复制实现的,属 ...

  8. Hive SQL 分类

    题目: 请使用Hive SQL实现下面的题目. 下面是一张表名为user_buy_log的表,有三个字段,user(用户),grp(分组编号),time(购物时间). 需要将用户按照grp分组,对ti ...

  9. HTML/CSS: 如何制作未读信息图标

    最近公司项目中涉及到制作通讯界面中未读信息的带数字的红色圆点图标. 拿我目前的博客头像图片为例(其实就是谷歌图片中粗略挑了一张顺眼的图片),效果图如下: HTML代码如下: <img id=&q ...

  10. [物理学与PDEs]第1章第6节 电磁场的标势与矢势 6.3 例 --- 电偶极辐射

    1. 偶极子: 相距为 $l$, 带电量分别为 $\pm q$ 的一对电荷组成的系统. 称 $$\bex {\bf m}=q{\bf l} \eex$$ 为电偶极矩, 其中 ${\bf l}$ 为 $ ...