python文件操作练习实例购物车

购物车程序
新手上路,请多关照!

# Author:Qiang
"""
        程序:购物车程序2.0
        1.0
        需求: 1.启动程序后,让用户输入工资,然后打印商品列表
                    2.允许用户根据商品编号购买物品
                    3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
                    4.可随时退出,退出时,打印已购买商品和余额

        2.0
        需求: 1.用户入口
                ·商品信息存在文件里
                ·已购商品,余额记录
              2.商家入口
                ·可以添加商品,修改价格
"""

product_list = "product_list.txt"   # 存放商品列表的文本
username_list = {}  # 用于存储用户的用户名和余额
username_list1 = "username_list.txt"  # 存放用户名和余额

user_shopping = []  # 用于存储用户单个购买的商品和价格
user_shopping_list = []  # 用户存储用户的总体消费记录

while True:
    choice = input("请输入您是商家还是顾客 >>>: ")   # 获取输入
    if choice == "商家":                        # 判断是否为商家端
        print("您已进入商家端".center(50, "-"))
        while True:
            merchant_choice = input("请选择 “添加商品”, “查看商品”  >>>: ")              # 商家选择需要进行的选项
            if merchant_choice == "添加商品":                            # 进入修改商品界面
                with open(product_list, "a+") as f:
                    count4 = False
                    while True:
                        if count4:
                            break
                        merchant_input_product = input("请输入需要添加的商品 >>>: ")  # 奇数为商品,偶数为价格
                        merchant_input_product = merchant_input_product + '\n'                  # 换行
                        f.write(merchant_input_product)                                         # 写入
                        merchant_input_product_price = input("请输入改商品价格 >>>: ")             # 将价格写入第二行
                        merchant_input_product_price = merchant_input_product_price + '\n'      # 换行
                        f.write(merchant_input_product_price)                                   # 写入
                        print("添加成功".center(50, "-"))                                         # 提示添加成成功
                        while True:
                            merchant_choice2 = input("如果继续添加商品请按'c',返回上一层请按‘b’,退出请按‘q’ >>>: ")        # 让商家选择接下来的操作
                            if merchant_choice2 == 'c':                                           # 继续添加商品
                                break
                            elif merchant_choice2 == 'b':                                         # 返回上一层
                                count4 = True
                                break
                            elif merchant_choice2 == 'q':                                         # 退出程序
                                exit()
                            else:                                                                 # 如果输入的字符都不符合,则提示输入有误
                                print("您输入的有误,请重新输入")
            elif merchant_choice == "查看商品":                           # 进入查看商品界面
                count = 1                                                # 定义变量用于统计次数
                product2 = ""                                            # 定义变量用存储偶数行的价格
                f = open(product_list, "r")                              # 打开文件  只读
                while True:
                    product1 = f.readline()                              # 逐行读取
                    if product1 == '':                                   # 当读取到的行数为空时   结束读取
                        break
                    if count == 1:                                       # 第一次进入
                        product2 = product1.strip()                      # 保存商品到product2中
                    elif count == 2:                                     # 第二次进入
                        product1 = product2 + "  " + product1.strip()    # 把product1的价格 添加到 product2后 并赋值给 product1
                        print(product1)                                  # 输出商品和价格
                    else:
                        if (count % 2) != 0:                             # 当行数为奇数  即所在行数为价格
                            product2 = product1
                        else:
                            product1 = product2.strip() + "  " + product1.strip()
                            print(product1)
                    count += 1                             # 行数+1
                f.close()                                  # 当文本读取完时  关闭
                merchant_choice3 = input("返回上一层请按‘b’,退出请按‘q’ >>>: ")        # 让商家选择接下来的操作
                if merchant_choice3 == 'b':                                         # 返回上一层
                    continue
                elif merchant_choice3 == 'q':                                         # 退出程序
                    exit()
                else:                                                                 # 如果输入的字符都不符合,则提示输入有误
                    print("您输入的有误")
            elif merchant_choice == "b":
                break
            elif merchant_choice == "q":
                exit()
            else:
                print("您输入的有误,请重新输入")
    elif choice == "顾客":                      # 判断是否为用户端
        print("您已进入用户端".center(50, "-"))
        while True:
            user_choice4 = input("请选择登录或者注册 >>>: ")
            if user_choice4 == "登录":
                while True:
                    username = input("请输入您的用户名 >>>: ")
                    f = open(username_list1, "r")
                    count3 = False
                    while True:
                        product4 = f.readline()
                        if product4 == '':
                            break
                        product4 = product4.strip()
                        if username == product4:
                            count3 = True
                        elif count3:
                            product4 = int(product4.strip())
                            username_list[username] = product4  # 将username设置为字典的key,余额为value
                            break
                    f.close()
                    if count3:       # 判断用户名是否存在
                        if username_list[username] > 0:     # 判断该用户名有没有余额
                            salary = username_list[username]
                            print("Product List".center(50, "-"))
                            count = 1  # 定义变量用于统计次数
                            product2 = ""  # 定义变量用存储偶数行的价格
                            f = open(product_list, "r")  # 打开文件  只读
                            while True:
                                product1 = f.readline()  # 逐行读取
                                if product1 == '':  # 当读取到的行数为空时   结束读取
                                    break
                                if count == 1:  # 第一次进入
                                    product2 = product1.strip()  # 保存商品到product2中
                                elif count == 2:  # 第二次进入
                                    product1 = \
                                        product2 + "  " + product1.strip()  # 把product1的价格 添加到 product2后 并赋值给 product1
                                    print(product1)  # 输出商品和价格
                                else:
                                    if (count % 2) != 0:  # 当行数为奇数  即所在行数为价格
                                        product2 = product1
                                    else:
                                        product1 = product2.strip() + "  " + product1.strip()
                                        print(product1)
                                count += 1  # 行数+1
                            f.close()  # 当文本读取完时  关闭
                            print("返回上一层请按‘b’,退出请按‘q’ >>>: ")
                            print("-".center(50, '-'))
                            while True:
                                user_choice1 = input("请输入您想要购买的商品 >>>: ")  # 让顾客选择接下来的操作
                                if user_choice1 == 'b':  # 返回上一层
                                    break
                                elif user_choice1 == 'q':  # 退出程序
                                    exit()
                                else:
                                    f = open(product_list, "r")  # 打开商品列表
                                    count2 = False                  # 用于控制商品名字读取后 再循环一次
                                    while True:
                                        product3 = f.readline()      # 将文本中读取的字符赋值给product3
                                        if product3 == "":           # 如果为空则跳出循环结束读取
                                            break
                                        product3 = product3.strip()    # 去掉从文本中读取出字符的空格
                                        if user_choice1 == product3:      # 判断用户输入的商品是否与文本中对应
                                            user_shopping.append(user_choice1)      # 对应则添加到用户单个购买的商品和价格列表
                                            count2 = True
                                        elif count2:                         # 用于控制商品名字读取后 再循环一次  保存商品的价格
                                            product3 = int(product3.strip())          # 令价格强类型转换为整数
                                            if username_list[username] > product3:         # 判断用户的余额是否大于商品的价格
                                                username_list[username] = \
                                                    username_list[username] - product3   # 如果大于则用余额减去商品价格完成扣费
                                                print("购买成功".center(50, "-"))
                                                user_shopping.append(product3)         # 将购买商品的价格添加到用户单个购买的商品和价格列表
                                                print("已成功将\033[32;1m%s\033[0m放入购物车,您的余额还剩\033[31;1m%s\033[0m元" %
                                                      (user_choice1, username_list[username]))   # 输出提示用户所剩余额
                                                user_shopping_list.append(user_shopping)   # 将用户单个购买的商品和列表添加到已购商品总列表
                                                user_choice2 = input("按任意键继续购买商品,按‘q’退出 >>>: ")
                                                if user_choice2 == 'q':   # 如果推出则打印用户购买的商品
                                                    if user_shopping_list == "":   # 判断已购商品总列表是否为空从而判断用户是否购买商品
                                                        print("你没有购买任何物品")
                                                    else:
                                                        print("已购商品".center(50, "-"))
                                                        for index, i in enumerate(user_shopping_list):   # 打印已购商品
                                                            print(index + 1, i)
                                                        print(user_shopping_list)
                                                        print("共计花费:%s元" % (salary-username_list[username]))  # 输出共计花费
                                                        print("".center(50, "-"))
                                                    print("您的余额还剩%s元" % (username_list[username]))
                                                    exit()
                                                else:
                                                    break
                                            else:
                                                print("您的余额不足!,只剩%s元" % (username_list[username]))
                                                user_choice3 = input("按任意键继续购买商品,按‘q’退出 >>>: ")
                                                if user_choice3 == 'q':  # 如果推出则打印用户购买的商品
                                                    if user_shopping_list == "":  # 判断已购商品总列表是否为空从而判断用户是否购买商品
                                                        print("你没有购买任何物品")
                                                    else:
                                                        print("已购商品".center(50, "-"))
                                                        for index, i in enumerate(user_shopping_list):  # 打印已购商品
                                                            print(index + 1, i)
                                                        print("共计花费:%s元" % (salary - username_list[username]))  # 输出共计花费
                                                        print("".center(54, "-"))
                                                    print("您的余额还剩%s元" % (username_list[username]))
                                                    exit()
                                                else:
                                                    break
                                    f.close()       # 关闭文件
                                    if count2 == False:           # 如果定义用来控制读取第二回循环count2 没有被改变,则该商品不在列表
                                        print("您输入的商品不在列表中,请重新输入")
                        else:
                            print("您的余额不足!")
                    else:    # 用户名不存在则进行注册
                        print("您输入的用户名不存在!")
            elif user_choice4 == "注册":
                with open(username_list1, "a+") as f:
                    register_username = input("请输入注册的用户名 >>>: ")         # 用户名
                    register_username = register_username + '\n'  # 换行
                    f.write(register_username)  # 写入
                    register_salary = input("请输入您的余额 >>>: ")  # 将用户余额写入第二行
                    register_salary = register_salary + '\n'  # 换行
                    f.write(register_salary)  # 写入
                    print("注册成功".center(50, "-"))
    else:
        print("您的输入错误,请重新输入")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值