码迷,mamicode.com
首页 > 编程语言 > 详细

06->python购物车

时间:2019-05-02 18:19:31      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:产品   class   警告   items   end   dig   inpu   price   while   

# 购物

produce = [
    {name: apple, price: 20},
    {name: mi, price: 30},
    {name: mac, price: 15}
]
shopping_car = {}
exit_flag = False
print("----------商品列表----------")
money = int(input("看我看看你的钱:").strip())
# if money.isdigit():
#     money = int(money)
# else:
#     print("请重让我看看你的钱!!")
#     money = input("看我看看你的钱").strip()
while not exit_flag:
    for index, i in enumerate(produce):
        print(序号{},\t商品:{},\t价格:{}.format(index, i[name], i[price]))
    choice = input("请选择你要购买产品的序号:")
    if choice.isdigit():
        choice = int(choice)
        if 0 <= choice < len(produce):   # 这里不推荐这么写,为了pycharm不出警告
            num = input("你要购买商品的数量:")
            money = money - int(produce[choice][price])*int(num)
            if money >= 0:
                # shopping_car.append(produce[choice][‘name‘])
                shopping_car[produce[choice][name]] = num
                # print("你购买的商品数量为%d" % len(shopping_car))
                for k, v in shopping_car.items():
                    print(购买商品:{},\t数量:{}.format(k, v))
                print(余额%d % money)

            else:
                print("穷鬼,回家拿钱去吧!!")
                break
    elif choice.upper() == Q:
        for k, v in shopping_car.items():
            print(购买商品:{},\t数量:{}.format(k, v))
        print(余额%d % money)
        exit_flag = True
    else:
        print("请选择产品的序号!!")

还可以优化通过while判断money、num

# 购物改进

produce = [
    {name: apple, price: 20},
    {name: mi, price: 30},
    {name: mac, price: 15}
]
shopping_car = {}
exit_flag = False
print("----------商品列表----------")
while not exit_flag:
    money = input("看我看看你的钱:").strip()
    if money.isdigit():     # 对用户输入的money进行出来
        money = int(money)
        break   # 结束当前循环
    else:
        print("请重让我看看你的钱!!")
        # money = input("看我看看你的钱").strip()
while not exit_flag:
    for index, i in enumerate(produce):
        print(序号{},\t商品:{},\t价格:{}.format(index, i[name], i[price]))
    choice = input("请选择你要购买产品的序号:")
    if choice.isdigit():
        choice = int(choice)
        if 0 <= choice < len(produce):   # 这里不推荐这么写,为了pycharm不出警告
            while not exit_flag:
                num = input("你要购买商品的数量:").strip()
                if num.isdigit():       # 对用户输入的num进行处理
                    break
                else:
                    print("请重新输入购买数量,靓仔!!!")
            money = money - int(produce[choice][price])*int(num)
            if money >= 0:
                # shopping_car.append(produce[choice][‘name‘])
                shopping_car[produce[choice][name]] = num
                # print("你购买的商品数量为%d" % len(shopping_car))
                for k, v in shopping_car.items():
                    print(购买商品:{},\t数量:{}.format(k, v))
                print(余额%d % money)

            else:
                print("穷鬼,回家拿钱去吧!!")
                break
    elif choice.upper() == Q:
        for k, v in shopping_car.items():
            print(购买商品:{},\t数量:{}.format(k, v))
        print(余额%d % money)
        exit_flag = True
    else:
        print("请选择产品的序号!!")

 

06->python购物车

标签:产品   class   警告   items   end   dig   inpu   price   while   

原文地址:https://www.cnblogs.com/wt7018/p/10802700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!