Python购物车

- Python之购物车

随手写的,没怎么优化,良心代码,改改文件路径就能用


流程图:
在这里插入图片描述
在我的代码里面用来三个TXT文件,一个用来存商品,一个用来存钱,一个用来存你买的东西。
**注意:**存钱的文件,在代码里第一次输入之后,下次就不需要输入了,自动读取上次的余额。存你买的东西的文件,只限于当前,方便打小票,以前买的东西不在记录。
**注意:**如果你要使用的话,一定要改文件的路径,我的在E盘,你要自己创建:就是改代码开头的file1(存商品),file2(存余额),file3(存小票),我的file文件:product_price.txt:
0 book 30
1 bike 300
2 IceStream 10
3 pen 245
4 paper 20
5 computer 1200

hava_bought.txt:(必须没有内容)
salary.txt:(可以不写,代码里自动要求你输入)
改这三个就可以用了。
代码如下:

# Author:dongdong
import os
file1 = "E:/product_price.txt"
file2 = "E:/salary.txt"
file3 = "E:/hava_bought.txt"
salary = 0
print("欢迎进入商家入口".center(50,"*"))
def alter_product():
        true = True
        while true:
            with open(file1, "r")as f1:
                _product = input("请输入需要修改的商品:(如:book)\n")
                found_product = 0
                lines = f1.readlines()
                for line in lines:
                    (number,product,price) = line.strip("\n").split(" ")
                    if product == _product:
                        found_product = 1
            f1.close()#如果输入商品正确,found_product == 1
            with open(file1, "r")as f3,open("%s.bak"%file1,"a+",encoding = "utf-8")as f2:
                if found_product == 1:
                    _price = input("请输入商品的价格:\n")
                    if _price.isdigit():
                        _price = int(_price)
                        lines = f3.readlines()
                        for line in lines:
                            (number, product, price) = line.strip("\n").split(" ")
                            if product == _product:
                                price = str(_price)
                            f2.write("%s %s %s\n"%(number,product,price))
                    else:
                        print('输入的啥玩意啊')#found_prouct == 1,写入文件,没有完全弄好
                        break
            if found_product == 1:
                os.remove(file1)
                os.rename("%s.bak"%file1,file1)
                print("修改成功")
            if found_product == 0:
                print("没有找到输入的商品")
            goon = input("继续输出请按 1,退出请按 q\n")
            if goon.isdigit():
                goon = int(goon)
                if goon == 1:
                    pass
                else:
                    print("草拟吗的,乱输个毛线,卡死你个龟孙子")
            elif goon == "q":
                true = False
            else:
                print("又他妈给老子乱输入")
def add_product():
    true = True
    while true:
        with open(file1, "r")as f1, open("%s.bak" % file1, "a+", encoding="utf-8")as f2:
            _input1 = input("输入产品:(格式:product)")
            _input2 = input("输入产品价格:")
            if _input2.isdigit():
                _input2 = int(_input2)
            else:
                print("垃圾输入,SB")
                break
            lines = f1.readlines()
            i = 0
            for line in lines:
                (number, product, price) = line.strip("\n").split(" ")
                i += 1
                f2.write(line)
            i = str(i)
            f2.write("%s %s %s\n" % (i, _input1,_input2))
        os.remove(file1)
        os.rename("%s.bak" % file1, file1)
        f1.close()
        f2.close()
        print("添加成功")
        goon = input("继续添加请按 1\n退出请按 q\n")
        if goon.isdigit():
            goon = int(goon)
            if goon == 1:
                pass
        elif goon == 'q':
            true = False
        else:
            print("瞎几把乱输入")
#商家模式入口
while True :
    _input = input("Please input the number:1:查看商品 2:修改或添加商品 q:退出\n")
    if _input.isdigit():
        _input = int(_input)
        if _input == 1:
            with open(file1, "r", encoding="utf-8")as f1:
                lines = f1.readlines()
                for line in lines:
                    (number, product, price) = line.strip("\n").split()
                    print(number, product, price)
            f1.close()
        elif _input == 2:
            _input1 = input("修改商品价格请按 1:\n添加商品请按 2:\n")
            if _input1.isdigit():
                _input1 = int(_input1)
            if _input1 == 1:
                alter_product()
            elif _input1 == 2:
                add_product()
            else:
                print("Your input is error!")
        else:
            print("眼瞎,乱输啊你")
            continue
    elif _input == "q":
        break
    else:
        print("your input is error!SB")
#客户模式入口
def check_banlance():
    global salary
    while True:
        with open(file2, "r", encoding="utf-8")as f1:
            salary = f1.readline()
            if salary == "":
                salary = input("请输入您的零花钱:")
                if salary.isdigit():
                    salary = str(salary)
                    f2 = open(file2, "w", encoding="utf-8")
                    f2.write(salary)
                    f2.close()
                    salary = int(salary)
                else:
                    print("瞎几把乱输")
                    continue
            else:
                salary = int(salary)
                print("您的余额:{a}".format(a=salary))
            return
def readBuy_product():
    global salary
    number = 0
    with open(file1,'r',encoding = "utf-8")as f1:
        lines= f1.readlines()
        for line in lines :
            (number,product,price) = line.strip("\n").split(" ")
            print(number,product,price)
        f1.close()
        _input = input('请输入商品编号进行购买,按q退出:\n')
        if _input.isdigit():
            _input = int(_input)
            number = int(number)
            if _input >-1 and _input<= number:
                with open(file1, 'r', encoding="utf-8")as f1:
                    lines = f1.readlines()
                    found = 0
                    for line in lines:
                        (number1, product, price) = line.strip("\n").split(" ")
                        number1 = int(number1)
                        if _input == number1:
                            found= 1
                            price = int(price)
                            if salary>=price :
                                salary = salary - price
                                print("购买成功,您的余额%s"%(salary))
                                f = open(file3,"a+",encoding="utf-8")
                                f.write("%s %s\n"%(product,price))
                                f.close()
                                return
                            else:
                                print("钱不够")
                    f1.close()
                    if found == 0:
                        print('输入编号错误')
            else:
                print("编号怎么输的,SB")
                return
        elif _input == 'q':
            return
        else:
            print("尼玛,能不能别瞎输入")
def print_back():
    global salary
    all = 0
    with open(file3, "r", encoding="utf-8")as f2:
        if f2.readline() != "":
            print("您的小票".center(15, "*"))
        else :
            return
        with open(file3, "r", encoding="utf-8")as f2:
            lines = f2.readlines()
            for line in lines:
                (product,price)= line.strip("\n").split()
                price = int(price)
                all = all + price
                line = line.strip("\n")#返回值类型,我草
                print(line)
            print("总共花费:%s元"%(all),"您的余额:%s元"%(salary))
        f2.close()
        f3 =  open(file3,"w",encoding="utf-8")
        f3.close()
print("欢迎进入客户入口".center(50,"*"))
check_banlance()
while True:
    _input = input("按 1 购买商品\n按 2 查看余额\n按 q 退出\n")
    if _input.isdigit():
        _input = int(_input)
        if _input == 1:
            readBuy_product()
        elif _input == 2:
            print("您的余额:%s"%(salary))
        else:
            print('瞎几把乱输!')
    elif _input == 'q':
        break
    else:
        print("瞎几把乱输!!!")
    f = open(file2,"w",encoding="utf-8")
    salary = str(salary)
    f.write(salary)
    f.close()
    salary = int(salary)
print_back()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值