Week2-作业:购物车程序优化(写文件方式)

#!/usr/bin/en python
# Author:lijun

'''
购物车:
    用户入口:
    1、商品信息存在文件里
    2、己购商品,余额记录。第一次启动时才需要输入工资
    
    商家入口:
    1、可以添加商品,修改商品价格
    
iphone,6000
macbook,12000
book,88
bike,900
'''

import  os

goods_file = os.getcwd() + os.sep + "goods_list.txt"
userinfo = os.getcwd() + os.sep + "userinfo.txt"
bought_goods = os.getcwd() + os.sep + "bought_goods.txt"

exit_shop = False
while not  exit_shop:
    select = input("请选择入口,用户叫输入1,商家请输入2,输入q退出:")
    if select == "1":
        print("-->用户入口")
        if not os.path.exists(userinfo) or not os.path.getsize(userinfo):
            #print("您是第一次购买商品")
            salary = input("请输入您的工资:")
        else:
            with open(userinfo) as user:
                salary=user.read()
        while salary == "":
            salary = input("请输入您的工资:")
            with open(userinfo,'w+') as user:
                user.write(salary)
        if salary.isdigit():
            salary=int(salary)
            with open(goods_file) as goods:
                goods = goods.read().splitlines()
                goods_list = []
                for i in goods:
                    g = i.split(',')
                    goods_list.append(g)
                for i,j in enumerate(goods_list):
                    goods_name = goods_list[i][0]
                    goods_price = goods_list[i][1]
                    print("商品编号:{index},商品名称:{name},商品价格:{price}" .format(index=i,name=goods_name,price=goods_price))
                print("您的余额为:%s" %salary)
                while not  exit_shop:
                    want_buy = input("请输入您要购买的商品编号,输入q退出:")
                    if want_buy.isdigit():
                        want_buy = int(want_buy)
                        if want_buy < len(goods_list) and want_buy>=0:
                            price=int(goods_list[want_buy][1])
                            if price <= salary:
                                with open(bought_goods,"a") as bought:
                                    bought.write(goods_list[want_buy][0]+"\n")
                                salary -= price
                                with open(userinfo,"w") as f:
                                   f.write(str(salary))
                                print("您购买了商品:%s,余额:%d" %(goods_list[want_buy][0],salary))
                            else:
                                print("您的余额为%s,买不起%s" % (salary, goods_list[want_buy][0]))
                        else:
                            print("输入错误,请输入正确的商品编号")
                    elif want_buy == "q":
                        exit_shop=True
                    else:
                        continue
    elif select == "2":
        print("商家入口")
        while not  exit_shop:
            seller = input("添加商品请输入1,修改商品价格请输入2,退出请输入q:")
            if seller == "1":
                good_add = input("请输入商品名称和价格,用逗号隔开:")
                with open(goods_file,"a+") as f:
                    f.writelines("\n" + good_add)
                print("添加商品成功...")
                continue
            elif seller == "2":
                with open(goods_file) as goods:
                    goods = goods.read().splitlines()
                    goods_list = []
                    for i in goods:
                        g = i.split(',')
                        goods_list.append(g)
                    for i, j in enumerate(goods_list):
                        goods_name = goods_list[i][0]
                        goods_price = goods_list[i][1]
                        print("商品编号:{index},商品名称:{name},商品价格:{price}".format(index=i, name=goods_name,price=goods_price))

                while not  exit_shop:
                    select_good = input("请输入修改的商品编号,退出请按q:")
                    if select_good.isdigit():
                        select_good = int(select_good)
                        while not  exit_shop:
                            new_price = input("请输入商品的新价格:")
                            if new_price.isdigit():
                                with open(goods_file,'w') as f:
                                    goods[select_good]="{good_name},{good_price}" .format(good_name=goods_list[select_good][0],good_price=new_price)
                                    f.write('\n'.join(goods))
                                print("修改商品价格成功...")
                                break
                            elif new_price == "q":
                                exit_shop = True
                            else:
                                continue
                    elif select_good == "q":
                        exit_shop = True
                    else:
                        continue
            elif seller == "q":
                exit_shop = True
            else:
                continue
    elif select == "q":
        print("退出成功,再见!")
        exit_shop=True
    else:
        continue

 

转载于:https://www.cnblogs.com/pythonlee/p/9556728.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值