简易购物车系统

money = int(input("请输入预算:"))
shop = [['1','iphone8',5888],['2','kndle',988],['3','ps4 pro',2800],['4','psv',1200],
        ['5','psvtv',450],['6','ps4 controler',230],['7','mp3',100]]
i = 1#为了通过修改i 退出多重循环
allChoice = []
while(i):
    for num in range(len(shop)):  # 打印商品列表
        print(str(shop[num][0]).ljust(5), shop[num][1].ljust(20), str(shop[num][2]).ljust(10), )
    choice = input("请输入要加入购物车的商品编号:")
    choice = [int(it) for it in choice.split(' ')]
    allChoice += choice #choice是单次选择的商品列表,allchoice是所有选择的商品列表
    while(1):
        total = 0
        choiceSet = set(allChoice)#转换成集合,便于不重复计数
        for it in choiceSet:
            print(shop[it-1][0],shop[it-1][1],shop[it-1][2],'*',allChoice.count(it))
            total += shop[it-1][2]*allChoice.count(it)
        print("总计:",total,"余额:",money-total)
        print("---------------------------------\n"
              "1:继续选购 2:整理购物车 3:结算\n")
        option = int(input( "请选择:"))
        if option == 1:
            break
        elif option == 2:
            item_num = int(input("请输入要删除的商品"))
            allChoice.remove(item_num)#每次只会删除一个元素
            continue
        else:
            if money>=total:
                print("购物结束,余额为:",money-total)
            else:
                print("余额不足,还差金额:",total-money)
            i = 0
            break

 

posted @ 2019-05-28 23:01  狼少女丶  阅读(222)  评论(0编辑  收藏  举报