python练习2 购物车程序
生活随笔
收集整理的這篇文章主要介紹了
python练习2 购物车程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*-
# @Time : 2018/10/18 16:06
# @Author : Two Brother
# @EMAIL : yeluyide@163.com
# @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('用戶不存在')
# @Time : 2018/10/18 16:06
# @Author : Two Brother
# @EMAIL : yeluyide@163.com
# @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('用戶不存在')
轉載于:https://www.cnblogs.com/twobrother/p/9922660.html
總結
以上是生活随笔為你收集整理的python练习2 购物车程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es6-Set与Map
- 下一篇: 利用python把图片转换成代码