python练习2 购物车程序

#  -*-   coding: utf-8   -*-
# @Time : 2018/10/18 16:06
# @Author : Two Brother
# @EMAIL : [email protected]
# @FILE : shoppingcart.py
# @SOFTWARE : PyCharm

##################title#####################
#用户入口:
#1.商品信息存在文件里
##
#商家入口:
#2.可以添加商品,修改商品价格
############################################
with open('use','r+',encoding='utf-8') as fu:
dictfu = {}
lines1 = fu.readlines()
for line in lines1:
(key,value) = line.strip().split()
dictfu[key] = value
with open('commodity', 'r+', encoding='utf-8') as fc:
dictfc = {}
lines2 = fc.readlines()
for line in lines2:
(key,value1,value2) = line.strip().split()
dictfc[key] = {value1:value2}
with open('admin','r') as fa:
admin = fa.readlines()
for i in range(0,len(admin)):
admin[i] = admin[i].strip()
i = input("请输入您的账号>>>>>>>>>>")
if i in dictfu.keys():
lastmoney = int(dictfu[i])
print('我是会员%s,我的余额%s' % (i,lastmoney))
for n in dictfc:
print(n,dictfc[n])
buy = []
salecost = 0
while True:
ic = input("请输入您要购买的商品编号>>>>>>>")
if ic in dictfc.keys():
print("商品%s已经加入购物车,查看购物车请按S,直接支付请按P" % dictfc[ic])
buy.append(dictfc[ic])
for x in dictfc[ic]:
salecost = salecost + int(dictfc[ic][x])
continue
elif ic.upper() == 'S':
print("您的购物车信息如下,总计%s元,直接支付请按P" % salecost)
for y in buy:print(y)
continue
elif ic.upper() == 'P':
if salecost <= lastmoney:
lastmoney2 = lastmoney - salecost
print("您本次消费金额%s元,余额还剩余%s元!欢迎下次光临" % (salecost,lastmoney2))
with open('use', 'w', encoding='utf-8') as fu2:
for line in lines1:
if i in line:
line = line.replace(str(lastmoney),str(lastmoney2))
fu2.write(line)
break
else:
pi = input("您本次购物需要花费金额%s元,余额只有%s元,交易失败,重新购物请按B,充值请按R,退出请按其他键>>>>"%(salecost,lastmoney))
if pi.upper() == 'B':
continue
if pi.upper() == 'R':
print("充值功能待开放")
break
else:
break
else:
break

elif i in admin:
while True:
ai = input("请选择你要做的操作:A查看商品 B修改商品价格 C添加删除商品>>>>>")
if ai.upper() == 'A':
for o in dictfc:
print(o,dictfc[o])
continue
elif ai.upper() == 'B':
bi = input("请选择你要修改商品的商品编号>>>>")
if bi in dictfc:
print(dictfc[bi])
bi2 = input("请输入你要修改此商品的价格>>>>")
with open('commodity','w',encoding='utf-8') as fc2:
for line in lines2:
if bi in line:
for bi3 in dictfc[bi]:
line = line.replace(dictfc[bi][bi3],bi2)
fc2.write(line)
with open('commodity', 'r', encoding='utf-8') as fc3:
lines3 = fc3.readlines()
for line in lines3:
(key, value1, value2) = line.strip().split()
dictfc[key] = {value1: value2}
else:
print("商品编码输入有误,请重新输入")
continue
else:
print('程序退出')
break
else:
print('用户不存在')

猜你喜欢

转载自www.cnblogs.com/twobrother/p/9922660.html