day2 购物车
需求:
商家入口:
1、商品列表永久保存(暂时使用存储在文件,也可以使用sqlite)里。
2、商家可以增加商品,也可以修改商品价格
买家入口:
1、购物车信息永久保存,暂时使用存储在文件,也可以使用sqlite。
2、信息包括余额以及已经购买的商品的数量。
商家代码:
products.txt: iphone 400
block 1000
bike 200
cup 50
as 35
ff 78 productM.py: #!/usr/bin/env python
#Author:windtalker
import re
products={}
f = open('products.txt','r+')
contents = f.readlines()
f.close()
for con in contents:
lcon = con.split(' ')
products[lcon[0]] = int(lcon[1].replace('\n','')) print(products) while True:
inp = input('input your set:')
linp = re.split('\s+',inp)
if inp == 'q':
break
elif len(linp) ==2 and linp[1].isdigit():
products[linp[0]] = int(linp[1])
else:
print('input error')
f = open('products.txt','r+')
f.truncate()
for k in products:
print(k,products[k])
str ='''%s %d\n''' % (k,products[k])
f.write(str)
f.close()
买家代码:
#!/usr/bin/env python
#Author:windtalker
import re
fshopping = open('shoppingcart.txt','r+')
fprodcuts=open('products.txt','r')
products={}
lines=fprodcuts.readlines()
fprodcuts.close()
for line in lines:
pro = re.split('\s+',line)
products[pro[0]] = int(pro[1]) s_line= fshopping.readline()
carts={} if not s_line:
money_inp=input('show your money:')
if money_inp.isdigit():
total_money = int(money_inp)
else:
total_money = re.split('\s+',s_line)[1]
for nextline in fshopping.readlines():
res = re.split('\s+', nextline)
carts[res[0]] = int(res[1]) while True:
#for inx,pr in enumerate(products,1):
#print(inx,pr)
pros = list(enumerate(products.items(), 0))
#prolist = list(pros)
#print(pros)
for ind,pro in pros :
#print('why')
print('%d : %s' % (ind,pro))
shop_inp = input('please shopping:')
if shop_inp == 'q':
break
elif shop_inp.isdigit() and int(shop_inp) >= 0 and int(shop_inp) < len(pros):
tmpinfo = pros[int(shop_inp)]
if tmpinfo[1][0] not in carts.keys():
carts[tmpinfo[1][0]] = 1
else:
carts[tmpinfo[1][0]] += 1
total_money -= tmpinfo[1][1]
else:
print('error input') fshopping.write('total_money %d\n'% total_money)
for k in carts:
fshopping.write('%s %d\n' % (k, carts[k])) fshopping.close()
day2 购物车的更多相关文章
- (转)Python作业day2购物车
Python作业day2购物车 原文:https://www.cnblogs.com/spykids/p/5163108.html 流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现) ...
- Python之路 day2 购物车小程序1
#Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...
- Python作业day2购物车
流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现),查询余额. 撸了两天一夜的代码,不多说,直接上码,注释神马的后面再说 #!/usr/bin/env python # -*- co ...
- python3.x Day2 购物车程序练习
购物车程序: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4. ...
- Python开发【第二章】:Python的数据类型
基本数据类型 一.整型 如: 18.73.84 整型具备如下功能: class int(object): """ int(x=0) -> int or long i ...
- Python开发【第二章】:数据类型
基本数据类型 一.整型 如: 18.73.84 整型具备如下功能: class int(object): """ int(x=0) -> int or long i ...
- Python3.5 Day2作业:购物车程序
需求: 1. 启动程序后,用户通过账号密码登录,然后打印商品列表. 2. 允许用户根据商品编号购买商品. 3. 用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒充值. 4. 可随时退出,退出 ...
- Python培训12期-day2作业-购物车
#!/usr/bin/env python import sys import os import getpass 商品={ '图书': { "<Puppet实战>": ...
- python基础day2作业:购物车
#功能:1.可注册账号2.登录买家账号3.可查询编辑购物车里商品4.可以余额充值5.可提示余额不足6.购物车结算 #使用:1.第一次使用先注册账号填写账号金额2.账号金额信息保存在buyer_acco ...
随机推荐
- webviewplugin
https://blog.csdn.net/qq_39197547/article/details/85007418 https://www.cnblogs.com/pjl43/p/9866753.h ...
- linux awk用法
awk是一个强大的文本分析工具,在对数据进行分析并生成报告时显得尤为强大. 使用方法:awk [options] 'BEGIN{ commands } pattern{ commands } END ...
- Bugku-CTF之never give up
Day23 never give up http://123.206.87.240:8006/test/hello.php 本题要点:url编码,base64编码,代码审计,php函数 ...
- autoit脚本-从基本的函数用法开始
适配浏览器:目前了解的有ie浏览器 MsgBox 显示可选提示超时的消息框 _ArrayDisplay _arraydisplay($aArray) ;$aArra一般为数组,方法用于展示表格展示数 ...
- Python3 tkinter基础 Radiobutton 创建三个单选钮
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【做题】ZJOI2017仙人掌——组合计数
原文链接 https://www.cnblogs.com/cly-none/p/ZJOI2017cactus.html 给出一个\(n\)个点\(m\)条边的无向连通图,求有多少种加边方案,使得加完后 ...
- Redis 集群版
1.# yum install ruby -y 1.1 后面需要用到ruby脚本 2.# yum install rubygems -y 1.1 安装ruby包管理器 3.# gem install ...
- vue-cli 使用 webpack-bundle-analyzer
# build for production and view the bundle analyzer report npm run build --report
- Request method 'PUT'/ 'POST' not supported
起因 在项目中遇到需要进行crud操作的功能,用的是Springboot+MybatisPlus+MySQL+AVue,在通过postman测试接口正确性时遇到此错误. 排查过程 因为项目运行是没问题 ...
- 使用bootstrap-select有时显示“Nothing selected”
.html()后加 $('#courseList').selectpicker('refresh'); $('#courseList').selectpicker('render'); 来源