购物车

 
# incoding=gbk

def show_commodity_list():
    """显示商品清单"""
    msg = "----------商品清单---------- "
    print(msg)
    for i in commodity_list:
        print(str(commodity_list.index(i)+1) + '. ', end='\t')
        for j in commodity_list[commodity_list.index(i)]:
           print('{0:<15}'.format(j), end='')
        print()

def show_shopping_list():
    """显示购物清单及余额"""
    msg = "----------你的购物清单----------"
    print(msg)
    for i in shopping_list:
        print(str(shopping_list.index(i) + 1) + '. ', end='')
        for j in shopping_list[shopping_list.index(i)]:
            print('{0:<15}'.format(j), end='')
        print()
    print("\n你的余额为" + str(saving) + '元。')


#商品清单
commodity_list = [
    ['computer', 5800],
    ['bicycle', 1500],
    ['coffee', 350],
    ['python book', 800],
    ['ipad',3000],
     ]
shopping_list = []            #购物车
selective_flag = True         #选择商品标记

show_commodity_list()

while selective_flag:
    saving = input("请输入你的本金(¥)(输入 q 停止购物):")
    if saving.isdigit() and int(saving) >= 0:
        saving = int(saving)
        print(saving)
        while selective_flag:
            product_number = input("\n请输入你需要购买商品的编号(输入 q 退出选择): ")

            if product_number.isdigit() and 0 < int(product_number) <= len(commodity_list):
                product_number = int(product_number)

                if saving >= commodity_list[product_number-1][1]:
                    shopping_list.append(commodity_list[product_number-1])
                    saving -= commodity_list[product_number-1][1]
                else:
                    print("对不起,您的余额不足!")

            elif product_number == 'q':
                selective_flag = False
                print("欢迎下次光临!\n")

            else:
                print("输入有误,请重新输入!")

        show_shopping_list()
    elif saving == 'q':
        selective_flag = False
        print("欢迎下次光临!\n")
    else:
        print("输入有误,请重新输入!")
        continue

 

posted @ 2020-10-13 16:55  tzg_雨中人  阅读(94)  评论(0)    收藏  举报