python购物车简单实现编写

伪代码:

可随时退出启动后让用户输入工资
然后打印列表
允许用户根据商品编号购买商品
用户选择商品后
检测余额是否足够 够就扣,不够就提示
退出时,打印一下购买的商品和余额

代码实现:

product_list = [
    ("MAX",9000),
    ("littlecar",520000),
    ("dress",249),
    ("python book",75),
    ("bike",2000),
]

saving = input("please   input  your  money:")   #变量进行判断

shopping_car = []


if saving.isdigit():
    saving = int(saving)
    while True:
        print("商品列表".center(50, "="))
        print("编号".center(10, "*"), "名称".ljust(27, "*"), "价格".ljust(6, "*"))

        for i in enumerate(product_list):      # , 1  为了从1编号开始
            print(str(i[0]).center(10, " "), str(i[1][0]).ljust(30, " "), str(i[1][1]).ljust(15, " "))
        choice = input("请选择您要购买的商品编号:[q:退出]")
        if  choice.isdigit():
            choice = int(choice)
            if  choice >= 0 and choice < len(product_list):
                p_item = product_list [choice]
                if p_item [1]< saving:
                    saving -= p_item[1]
                    shopping_car.append(p_item)
                else:
                    print("您余额不足,还剩%s"%saving)
                    print("已购买商品列表".center(50, "="))
                    print(p_item)
            else:
                print("请选择存在的编号")
        elif choice == "q":
            for i in shopping_car:
                print(i)
            print("您还剩%s元钱"%saving)
            break
else:
    print("please input number[0-9]")

你可能感兴趣的:(Python)