简单购物车

简单购物车

#定义一个嵌套列表
product_list = [('mac',9000),
                ('bike',2000),
                ('book',100),
                ('tasila',900000)
                ]
#定义购物车
shopping_card = []
shopping_card2 = []
#定义本身的金额
saving = input("please input your money:")
#判断输入的金额是否合法
if saving.isdigit():
    saving = int(saving)
    #让不断购买商品
    while True:
        #打印列表内容
        for i,v in enumerate(product_list,1):
            print(i,'>>>',v)
        #定义购买编号
        choice = input('input your choice:[quit:q]')
        #判断输入是否数字
        if choice.isdigit():
            choice = int(choice)
            #判断商品个数
            if choice>0 and choice <= len(product_list):
                #定义商品编号
                Price = product_list[choice-1]
                #判断商品和本金的差额
                if Price[1] < saving:
                    saving -= Price[1]
                    shopping_card.append(Price)
                    print("你购买了%d号商品,价格是%s元,还剩%s元" %(choice,Price[1],saving))
                    print("-" * 30)
                    print("请继续选购")
                    print()
                else:
                    print("余额不足,还剩%s元,请充值。" %saving)
                    print("-" * 30)
                    print("")
            else:
                print("商品编号不存在")

        elif choice =='q':
            print("******你购买了如下商品*******")
            for i in shopping_card:
                if i not in shopping_card2:
                    shopping_card2.append(i)
                    print(i,"x",shopping_card.count(i))
            print("还剩%s元" %saving)
            break
        else:
            print("亲,你输入了非法字符")
else:
    print("亲,你输入了非法字符")

测试结果:git

please input your money:111111
1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]1
你购买了1号商品,价格是9000元,还剩102111元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]1
你购买了1号商品,价格是9000元,还剩93111元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]2
你购买了2号商品,价格是2000元,还剩91111元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]2
你购买了2号商品,价格是2000元,还剩89111元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]3
你购买了3号商品,价格是100元,还剩89011元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]3
你购买了3号商品,价格是100元,还剩88911元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]4
余额不足,还剩88911元,请充值。
------------------------------

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]4
余额不足,还剩88911元,请充值。
------------------------------

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]3
你购买了3号商品,价格是100元,还剩88811元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]2
你购买了2号商品,价格是2000元,还剩86811元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]3
你购买了3号商品,价格是100元,还剩86711元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]2
你购买了2号商品,价格是2000元,还剩84711元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]1
你购买了1号商品,价格是9000元,还剩75711元
------------------------------
请继续选购

1 >>> ('mac', 9000)
2 >>> ('bike', 2000)
3 >>> ('book', 100)
4 >>> ('tasila', 900000)
input your choice:[quit:q]q
******你购买了如下商品*******
('mac', 9000) x 3
('bike', 2000) x 4
('book', 100) x 4
还剩75711元