1. 程式人生 > >python練習2 購物車程式

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('使用者不存在')