Python笔记(三)购物车小程序

product_list = [
    ['Apple',10],
    ['Banner',6],
    ['Peach',12],
    ['Orage',14],
    ['Grape',11]
]
shopping_list = []
salary = input("请输入你的余额:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index, item in enumerate(product_list):
            print(index,item)
        user_choice = input("请输入商品的编号:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >= 0:
                p_item = product_list[user_choice]
                if p_item[1] < salary:
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("您购买的%s已加入购物车,余额剩余:\033[31;1m%s\033[0m" %(p_item,salary))
                else:
                    print("\033[41;1m你的余额不足!\033[0m")
            else:
                print("找不到相应的商品编号!")
        elif user_choice == 'q':
            print("购物车内的商品如下:%s\n您的余额剩余:%s" %(shopping_list,salary))
            for i in shopping_list:
                print(i)
            exit()
        else:
            print("您输入的选项错误!")
else:
    print("Error!")

你可能感兴趣的:(Python)