从零单排Python学习第二课

简易购物车:

1,商品展示

2,添加商品到购物车

#list集合保存商品信息
product_list = [("电视机",5000), ("洗衣机",3000), ("电冰箱",2000), ("电吹风",1000), ("电风扇",300), ("热水器",2900)]
#购物车数据 shopping_car
= []
#当前余额 salary
= 100000
#打印出商品信息 for index,item in enumerate(product_list): print(index,item) while True: print("---当前余额> %s 输入'q'退出程序 输入编号加入购物车---" % (salary)) choice = input("输入商品编号>>") if choice.isdigit():#判断输入是否是编号 choice = int(choice) if choice < len(product_list) and choice >= 0:#判断是否在下标范围内 if salary > product_list[choice][1]:#判断余额大于商品售价 salary = salary - product_list[choice][1]#计算余额 shopping_car.append(product_list[choice])#加入购物车 print("购物车商品>", shopping_car) else: print("余额不足") else: print("商品不存在") elif choice=="q": print("退出程序 》 购物车商品>", shopping_car) break else: print("输入错误") break

购物车增强版↓↓

商品信息保存在文件中

增加卖家入口 商品可以添加 改价格

with open("product.txt") as f:
    product_list = list(eval(f.readline()))
#product.txt文件如下
[("电视机",5000),("洗衣机",3000),("电冰箱",2000),("电吹风",1000),("电风扇",300),("热水器",2900)]
# Author ml

with open("product.txt") as f:#读取商品列表
    product_list = list(eval(f.readline()))
shopping_car = []
salary = 100000

while True:
    print("---当前余额> %s 输入'q'退出程序 输入'e'进入编辑模式 输入编号加入购物车---" % (salary))
    for index, item in enumerate(product_list):
        print(index, item)#商品展示
    choice = input("输入商品编号加入购物车>>")
    if choice.isdigit():#判断输入是否是编号
        mode= 0
        choice = int(choice)
        if choice < len(product_list) and choice >= 0:#判断是否在下标范围内
            if salary > product_list[choice][1]:#判断余额大于商品售价
                salary = salary - product_list[choice][1]
                shopping_car.append(product_list[choice])
                print("购物车商品>", shopping_car)
            else:
                print("余额不足")
        else:
            print("商品不存在")
    elif choice=="q":
            print("退出程序 》 购物车商品>", shopping_car)
            break
    elif choice=="e":
            mode = 1
            print("正在编辑模式 添加商品请输入 a","编辑商品价格请输入编号")
            order = input(">>>")
            if order.isdigit():
                order = int(order)
                if order < len(product_list) and order >= 0:  # 判断是否在下标范围内
                    print(product_list[order])
                    new_price = int(input("请输入价格>>"))
                    if new_price>0:
                        new_tuple = (product_list[order][0],new_price)
                        product_list.insert(order,new_tuple)
                        del product_list[order+1]
                        with open("product.txt", "w") as f:
                            f.write(str(product_list))
                    else:
                        print("输入价格不合法")
            elif order == "a":
                add_product = input("请输入商品名称及价格用'/'分割")#没有对新增加商品正确性做判断
                new_tuple = tuple((add_product.split("/")[0],int(add_product.split("/")[1])))
                product_list.insert(len(product_list),new_tuple)
                with open("product.txt", "w") as f:
                    f.write(str(product_list))
    else:
            print("输入错误")
            break

运行图↓↓

 

扫描二维码关注公众号,回复: 3208268 查看本文章

猜你喜欢

转载自www.cnblogs.com/limiaolei/p/9651930.html