>

5模拟购物车程序,购物车程序练习

- 编辑:金沙国际平台登录 -

5模拟购物车程序,购物车程序练习

购物车练习题: 供给如下 1、运营程序后,让顾客输入本身的金额,并打字与印刷出商品列表 2、让客商依据商品编号,选拔商品 3、选用成功后,检查测验余额是不是够,够就向来扣款并将货物保存至字典中,借使余额不足提醒客商进行充钱 4、客户能够每11日选用退出,退出时打字与印刷本人购买的商品 ,成本总额和余额

product_list = [
  ["台式电脑", 4992],
  ["手机", 2999], 
  ["移动硬盘", 390],
  ["U盘", 100], 
  ["笔记本", 6223],
  ["鼠标", 200], 
  ["键盘", 492]
]

####################################################
product_list = [["台式电脑", 4992],["手机", 2999], ["移动硬盘", 390],["U盘", 100], ["笔记本", 6223],["鼠标", 200], ["键盘", 492]]
shopping = {}
bbb= 0
# sho = {
#     "台式电脑":{"price":4992,"num":3}
# }
salary = input("请输入您的工资:")
if salary.isdigit():
    salary = int
    while True:
        # for i in product_list:
        #     print(product_list.index
        for a, b in enumerate(product_list):
            print
        user_choice = input("选择要买的商品序列
        if user_choice.isdigit():   # 判断输入的是否是数字
            user_choice = int(user_choice)  # 将str数字转为int类型
            if user_choice < len(product_list) and user_choice >= 0:  # 判断数字时候是列表序列的范围
                # 判断钱够不够
                p_item = product_list[user_choice]  # 这个是一个商品["台式电脑", 4992]
                if p_item[1] <= salary:  # 判断商品列表中的价格和你的工资的关系
                    if p_item[0] in shopping.keys():
                        shopping[p_item[0]]["num"] += 1
                        salary -= p_item[1]  # 将工资总数减去你选的商品的价格
                        print
                        print("Added %s into shopping cart, yu e is %s" % (p_item, salary))
                    else:
                        shopping[p_item[0]] = {"price":p_item[1],"num":1}  # 如果是你工资大于等于商品价格,则将商品加入到shopping空列表中
                        salary -= p_item[1]  # 将工资总数减去你选的商品的价格
                        print("Added %s into shopping cart, yu e is %s" %(p_item,salary))
                        print
                else:
                    print("资产不足,请充值:当前商品价格为:%s ,您资产剩余为: %s"%(p_item,salary))
                    chong = input("充值请输入c,结算请输入q,输入其他字符为无效")
                    if chong == "c":
                        b = input("请输入您要充值的金额:")
                        if b.isdigit():
                            b = int
                            salary += b
                            print("您充值资产为:%s ,当前资产为 %s ,购物车列表为:%s" %(b,salary,shopping))
                            continue
                    elif chong == "q":
                        print("您购买的商品有:%s ,您的资产剩余 %s" % (shopping, salary))
                        for k, v in shopping.items():
                            n = v["price"]
                            m = v["num"]
                            zong = n * m
                            bbb = bbb + zong
                        print("总消费为: %s" % 
                        #for k,v in cat.items():
                        # print
                        # n = v['num']
                        # m = v['one_proce']
                        # all_sum = n * m
                        # all_price = all_price + all_sum
                        exit()
                    else:
                        continue
            else:
                print("请输入正确的商品序号")
        elif user_choice.lower() == "q":
            print("您购买的商品有:%s ,您的资产剩余 %s" %(shopping,salary))
            for k,v in shopping.items():
                n = v["price"]
                m = v["num"]
                zong = n * m
                bbb = bbb + zong
            print("总消费为: %s" %
            exit()
        else:
            print("请输入正确的商品序号")

要求:

1、运转程序后,输入客户名密码后,让客商输入薪酬,然后打字与印刷商品列表

2、允许客商依据商品编号购买商品 

3、客户选拔商品后,检查实验余额是或不是够,够就径直扣款,非常不够就提醒

4、可每一日退出,退出时,打字与印刷已购销商品和余额 

 

思路:

1、index,和for循环商品列表

2、商品号要在商品系列内

3、剖断余额

4、打字与印刷所选商品及价位

goods =[
{"name":
"电脑", "price": 1999},
{"name":
"鼠标", "price": 10},
{"name":
"游艇", "price": 20},
{"name":
"美女", "price": 998},

]
shopping_list = []
wage = input('请输入你的工资:')

if wage.isdigit():
    wage=int(wage)

while True:
    for index,i in enumerate(goods):
        print(index,i)
    user_choice = input('请选择要买的编号>>:')
    if user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(goods) and user_choice >=0:
            goods_item = goods[user_choice]
            print(goods_item['price'])
            if goods_item['price'] < wage:#买的起
                shopping_list.append(goods_item)
                wage -=goods_item['price']
                print("添加 %s 到你的购物车,你的余额为 33[31;1m%s33[0m" %(goods_item,wage) )
            else:
                    print("33[41;1m你的余额不足[%s]啦33[0m" % wage)
        else:
                print("product code [%s] is not exist!"% user_choice)
    elif user_choice == 'q':
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("你的余额为:",wage)
            exit()
    else:
            print("invalid option")

  

本文由编程发布,转载请注明来源:5模拟购物车程序,购物车程序练习