Python实例---简单购物车Demo

简单购物车Demo

# version: python3.2.5
# author: ‘FTL1012‘
# time: 2017/12/7 09:16

product_list = (
    ['Java', 300],
    ['Oracle', 400],
    ['Mysql', 200],
    ['Python', 600],
    ['Shell', 150]
)

money = input("充值金额为: ")
shop_list = []
summary = 0
if money.isdigit():
    money = int(money)
    while True:
        print("---------欢迎来到HHH品牌旗舰店-----------")
        for k, v in enumerate(product_list):
            print(k, v)
        choice = input("请选择[q:结束]:")
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice < len(product_list):
                item = product_list[choice]
                print("你选择了%d号" % choice, '你选择的商品是%s' % item)
                shop_list.append(item)
                print("#####################################################################")
                print("你的购物车有\n%s" % shop_list, '\n共计%d' % len(shop_list), "件商品")
            else:
                print("选择错误,退出程序!!")
                break
        elif choice == 'q':
            for k, v in shop_list:
                summary += v
            print("欢迎您的光临,你一共需要支付金额:%d " % summary, "您的会员卡充值金额为%s" %money)
            # print("您的余额为%s " % str(money - summary))
            if summary > money:
                print("您的余额不足,请充值!!!")
                break
            else:
                print("---------您购买的商品如下-----------")
                print(shop_list)
                print("欢迎您的光临,你一共需要支付:")
                print("金额:%d " % summary, "您的余额为%s "% str(money - summary), "感谢您的光临,欢迎下次光临^..^")
                break
        else:
            print("exit")
else:
    print("零钱必须是整数!!!")
    exit(0)

 

image

image

posted @ 2018-07-28 14:26  小a玖拾柒  阅读(797)  评论(0编辑  收藏  举报