首页 技术 正文
技术 2022年11月16日
0 收藏 566 点赞 4,382 浏览 2880 个字
基本要求:
用户入口
1、商品信息存在文件里
2、已购商品、余额记录,长期保存, 即第一次启动是需要输入预算的,以后就拿剩下的钱买东西 商家入口
1、可以添加商品,删除商品,修改商品价格
2、按q可以退出实现:
1、用到list和dic
2、if判断 while和for循环
3、用文件记录商品信息和购物记录
'''
购物车
用户入口
、商品信息存在文件里
、已购商品、余额记录,长期保存,
即第一次启动是需要输入预算的,以后就拿剩下的钱买东西商家入口
、可以添加商品,修改商品价格按q可以退出
'''
import os
#定义函数
#客户端
def customer(budgetfile,productfile):
#显示或获取当前预算
budgetfd = open(budgetfile, 'r', encoding="utf-8")
print("目前已购买:")
cartdata=budgetfd.readline()
cartlist=[]
budget=''
while cartdata:
cartlist.append(cartdata.split(" ")[].strip())
print(cartdata.split(" ")[])
budget=cartdata.split(" ")[]
cartdata=budgetfd.readline() flag=True
if budget=='':
while flag:
budget=input("请输入购物预算: ")
if budget.isdigit():
budget=int(budget)
flag=False
else:
print("必须输入数字。")
continue
else:
print("您的余额是:"+budget)
budget = int(budget)
budgetfd.close() shopping=True
while shopping:
# 先显示当前商品列表
productfd = open(productfile, 'r', encoding='utf-8')
print("目前商品列表:")
data = productfd.readline()
datadic = {}
while data:
datadic[data.split(" ")[].strip()] = data.split(" ")[].strip()
print(data)
data = productfd.readline()
productfd.close() choice=input("输入你要买的商品名称,按q退出:")
if choice=='q':
shopping=False;
return True
elif choice in datadic:
if int(datadic[choice])<budget:
fd=open(budgetfile,'a',encoding='utf-8')
budget-=int(datadic[choice])
print("您的余额是:"+str(budget))
cartlist.append(choice)
fd.write(choice+' '+str(budget)+'\n')
fd.close()
print("目前已购买:")
for key in cartlist:
print(key)
else:
print("预算不足,不能购买。")
else:
print("不存在该商品。")#商户端
def boss(productfile):
#打印目前商品列表 fd=open(productfile,'r',encoding='utf-8')
fd.close() while True:
#先显示当前商品列表
fd.close()
fd = open(productfile, 'r+', encoding='utf-8')
print("目前商品列表:")
data = fd.readline()
datadic = {}
while data:
datadic[data.split(" ")[].strip()] = data.split(" ")[].strip()
print(data.split(" ")[].strip(), data.split(" ")[].strip())
data = fd.readline()
fd.close() operation = input("请输入您要做的操作:1表示增加商品、2表示删除商品、3表示修改商品价格、q表示退出程序: ")
if operation=='':
fd = open(productfile, 'a', encoding='utf-8')
string=input("请输入要添加的商品及价格,用空格分隔:")
key=string.split(" ")[].strip()
value=string.split(" ")[].strip()
if key not in datadic:
fd.write(string+'\n')
datadic[key]=value
else:
print("已存在该商品,不能添加")
fd.close()
elif operation=='':
string = input("请输入要删除的商品名称: ")
fd=open(productfile,'w',encoding='utf-8')
if string in datadic:
datadic.pop(string)
else:
print("要删除的商品不存在。")
for key in datadic:
fd.write(key + ' ' + datadic[key] + '\n')
fd.close()
elif operation=='':
string=input("请输入要修改的商品名称和价钱,用空格分隔:")
k=string.split(" ")[]
v=string.split(" ")[]
if k in datadic:
datadic[k]=v
else:
print("要修改的商品不存在。")
fd = open(productfile, 'w', encoding='utf-8')
for key in datadic:
fd.write(key+' '+datadic[key]+'\n')
fd.close()
elif operation=='q':
fd.close()
flag=False
return True
else:
print("请输入准确的命令。")
operation = input("请输入您要做的操作:1表示增加商品、2表示删除商品、3表示修改商品价格、q表示退出程序: ")#脚本主程序
exitflag=FalseIdent=input("您是客户还是商户:")
while not exitflag:
if Ident=="客户":
exitflag=customer("cartlist.txt","productlist.txt")
elif Ident=="商户":
exitflag=boss("productlist.txt")
elif Ident=="q":
exitflag=True
print("退出程序。")
else:
print("请输入准确的字眼。")
Ident = input("您是客户还是商户:")


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774