码迷,mamicode.com
首页 > 编程语言 > 详细

python week2 购物车优化

时间:2018-09-20 01:04:13      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:lse   file   退出   span   入口   update   inf   split()   lines   

‘‘‘
用户入口
1. 商品信息存在文件里
2. 已购商品,余额记录

商家入口
3. 可以添加商品,修改商品价格
‘‘‘

filePath = goodslist.txt
with open(filePath, r) as f:
    filetext=f.read()
filetextlines=filetext.split(\n) # 每行一个元素,不含换行符
# 提取商品列表
goods = list([])
for k in range(len(filetextlines)):
    c=filetextlines[k].split()
    goods.append([c[0],int(c[1])])
    
goodsLen = len(goods)
# 用户入口
salary=int(input("请输入您的工资: "))
balance = salary
print(编号,商品名称 ,价格)
for k in range(goodsLen):
    print(k,. ,goods[k][0],  ,goods[k][1])

orderList = list([])
while True:
    selectNum = int(input("请选择商品编号(-1退出):"))
    if selectNum < 0:
        break
    tempBalance = balance-goods[selectNum][1]
    if tempBalance < 0:
        print(余额不足)
    else:
        orderList.append(goods[selectNum])
        balance=tempBalance
print("您的购物清单:")
for k in range(len(orderList)):
    print(orderList[k]) 
print("您的余额为:%d"%(balance))

# 商家入口
goodskey = list(dict(goods).keys())
print(编号,商品名称 ,价格)
for k in range(goodsLen):
    print(k,. ,goods[k][0],  ,goods[k][1])
while True:
    updateInfo = (input("请选择更新商品(q退出,商品名:修改价格):"))
    print(updateInfo)
    if updateInfo==q:
        break
    else:
        try:
            ind = goodskey.index(updateInfo)
            goods[ind][1]=int(input(请输入价格: ))
        except:
            goods.append([updateInfo,int(input(请输入价格:))])    

goodsstr = ‘‘
for k in range(len(goods)):
    goodsstr += goods[k][0] +\t+str(goods[k][1])+\n
with open(filePath, w) as f:
    f.write(goodsstr)

 

python week2 购物车优化

标签:lse   file   退出   span   入口   update   inf   split()   lines   

原文地址:https://www.cnblogs.com/wuyouwuyou/p/9678412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!