简单的购物车系统

white 资源库 2018-06-16 60 次浏览 简单的购物车系统已关闭评论
print("----------------商品列表-------------")

products = [
    ['iphone8',6888],
    ['Macbook',3888],
    ['小米',6888],
    ['联想',6888],
    ['Nike',6888],
    ['coffee',6888]
]

for index,p in enumerate(products):
    print('%s. %s    %s '%(index,p[0],p[1]))

exit_flag = True
shopping_cart = []


while exit_flag:
    choice = input('请输入购买商品的编号(输入"q"退出):')
    if choice.isdigit():
        choice=int(choice)
        if choice < len(products):
            shopping_cart.append(products[choice])
            print('您购买的商品 %s 已加入购物车' % (products[choice]))
        else:
                print('商品不存在请重新输入')

    else:
        if choice =='q' and len(shopping_cart)>0:
            print('--------您购买的商品如下----------')
            for index,p in enumerate(shopping_cart):
                print('%s. %s    %s '%(index,p[0],p[1]))
            exit_flag = False

        elif choice == 'q':
            print('您已退出购物系统')
            exit_flag = False

        else:
            print('输入有误,请重新输入(输入"q"退出):')

 


products = [
    ['iphone8',1888],
    ['Macbook',3888],
    ['小米',2888],
    ['联想',5888],
    ['Nike',4888],
    ['coffee',6888]
]



salary = int(input('请输入工资:'))

products_cart = []
while True:
    for k,v in enumerate(products):
        print(k,v[0],v[1])
    choice = input('请输入产品编号:')
    if choice == 'q'and int(len(products_cart)) > 0:
        print('-----您已退出购物系统,您购买了以下商品------')
        for k,v in enumerate(products_cart):
            print(v[0],v[1])
        break
    elif choice.isdigit():
        index = int(choice)
        if salary >= int(products[index][1]):
            products_cart.append(products[index])
            salary = salary - int(products[index][1])
            print('您选购的商品%s已加入到购物车,余额为%s,请继续选购(输入q退出)' %(products[index],salary))
            print(products[index][1])
        else:
            print('您的余额不足,请充值后继续购买')
    else:
        print('输入有误,请重新输入')

 

回顶部