首页 > 编程语言 > 详细

python 购物车

时间:2018-08-15 23:54:04      阅读:240      评论:0      收藏:0      [点我收藏+]
goods_list = [
    {‘name‘: ‘苹果‘, ‘price‘: ‘10‘},
    {‘name‘: ‘香蕉‘, ‘price‘: ‘15‘},
    {‘name‘: ‘菠萝‘, ‘price‘: ‘20‘},
    {‘name‘: ‘西瓜‘, ‘price‘: ‘25‘},
    {‘name‘: ‘葡萄‘, ‘price‘: ‘30‘},
]
shopping_car = {}
print("欢迎光临小粉嫩水果店!")
money = input("请拿出您的银子:")
if money.isdigit() and int(money) > 0:
    flag = True
    while flag:

        for i, k in enumerate(goods_list):
            print(‘序号{}, 商品{}, 价格{}‘.format(i, k[‘name‘], k[‘price‘]))
        choose = input("请输入您选择的序号:")
        if choose.isdigit() and int(choose) < len(goods_list):
            num = input("请输入您要购买商品的个数:")
            if num.isdigit():
                if int(money) > int(goods_list[int(choose)][‘price‘]) * int(num):
                    money = int(money) - int(goods_list[int(choose)][‘price‘]) * int(num)
                    if goods_list[int(choose)][‘name‘] in shopping_car:
                        shopping_car[goods_list[int(choose)][‘name‘]] = shopping_car[goods_list[int(choose)][‘name‘]] + int(num)
                    else:
                        shopping_car[goods_list[int(choose)][‘name‘]] = int(num)
                    print("购物车中的商品有{}, 您的余额为{}".format(shopping_car, money))
                    print("如果您想离开请按Q或q,欢迎下次光临!")
                else:
                    print("穷鬼,滚去敲代码赚钱!")
                    break
            else:

                print("请输入数字,谢谢!")
        elif choose.upper() == "Q":
            flag = False
            print("您当前的购物车为{},余额为{}".format(shopping_car, money))

        else:
            print("傻逼,输入数字!!!")

 

python 购物车

原文:https://www.cnblogs.com/-1206-/p/9484526.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!