zoukankan      html  css  js  c++  java
  • Python之购物车

    Python之购物车

     1 msg_list = [
     2         ['iphone',8888],
     3         ['coffe',38],
     4         ['book',90],
     5         ['Tesla',100000],
     6         ['RR',1000000000]
     7     ]
     8 
     9 
    10 shopping_cart = []
    11 
    12 salary = int(input('input your salary:'))
    13 
    14 while True:
    15     #计数器,给列表的物品赋值
    16     index = 0
    17     # 循环列表msg_list,并打印
    18     for product in msg_list:
    19         print(index,product)
    20         index +=1
    21     #输入要选取的物品编号
    22     choice = input('>>:').strip()
    23     #对输入的类型进程判断,看是否是数字,isdigit是判断是否是数字的方法
    24     if choice.isdigit():
    25         #判断输入的数字是否在列表的索引值范围内
    26         choice = int(choice)
    27         if choice >= 0 and choice <= len(msg_list):
    28             #商品存在,取到商品。
    29             product = msg_list[choice]
    30             #判断输入的金额是否买得起。
    31             if product[1] <= salary:
    32                 #买得起,并加入购物车。
    33                 shopping_cart.append(product)
    34                 #总金额减去加入购物车的钱
    35                 salary -= product[1]
    36                 # 33[31;1m XXXXXXXX 33[0m  固定写法,可以改变XXXXXX的字体颜色
    37                 print('Added product:' + product[0] + ' into shopping cart, 33[31;1myour current33[0m balance:' + str(salary))
    38             else:
    39                 print('买不起了,产品价格是' + str(product[1]) + "还差" + str(product[1]-salary) + '')
    40         else:
    41             print('商品不存在')
    42     elif choice == 'q':
    43         print('-------已购买商品-------')
    44 
    45         for i in shopping_cart:
    46             print(i)
    47 
    48         print('您的余额为:',salary)
    49         print('---------结束----------')
    50         break
    51     else:
    52         print('无此选项')

    购物车优化:

     1 msg_list = [['IphoneX',8888],
     2             ['Coffee',30],
     3             ['MINI',300000],
     4             ['Python',998],
     5             ['LAND ROVER',4000000],
     6             ['RR',10000000],
     7             ]
     8 
     9 shopping_cart = {}
    10 
    11 salary = int(input("input your salary:"))
    12 
    13 while True:
    14     index = 0
    15     for product in msg_list:
    16         print(index,product)
    17         index +=1
    18     choice = input(">>:").strip()
    19     if choice.isdigit(): #判断是否为数字
    20         choice = int(choice)
    21         if choice >= 0 and choice < len(msg_list):#商品存在
    22             product = msg_list[choice] #取到商品
    23             if product[1] <= salary:   #判断能否买的起
    24                 #买的起
    25                 if product[0] in shopping_cart: #之前购买过呀
    26                     #字典的取值方法
    27                     shopping_cart[product[0]][1]  += 1 #[price, 数量],只需要把数量+1,加入购物车
    28                 else:
    29                     shopping_cart[product[0]] = [product[1], 1]  #创建一条商品购买记录
    30 
    31                 salary -= product[1] #扣钱
    32                 print("Added product " + product[0] + " into shopping cart,33[42;1myour current33[0m balance " + str(salary) )
    33             else:
    34                 print("买不起,穷逼! 产品价格是" + str(product[1])  + " 你还差" + str(product[1]-salary) + "")
    35         else:
    36             print("商品不存在!")
    37     elif choice == "q":
    38         print("-------已购买商品列表--------")
    39         #print(shopping_cart)
    40         id_counter = 1
    41         #初始化一个总花费的变量
    42         total_cost = 0
    43         print("id    商品    数量    单价    总价")
    44         for key in shopping_cart:
    45             #字符串格式化,10是每个占位符的距离  
    46             print("%10s%10s%10s%10s%10s" %(id_counter,
    47                   key,
    48                   shopping_cart[key][1],
    49                   shopping_cart[key][0],
    50                   shopping_cart[key][1]*shopping_cart[key][0]))
    51 
    52             id_counter +=1
    53             total_cost += shopping_cart[key][1]*shopping_cart[key][0] #单个商品总价
    54 
    55         print("您的总花费为:",total_cost)
    56         print("您的余额为:",salary)
    57         print("----------end----------")
    58         break
    59     else:
    60         print("无此选项!")
  • 相关阅读:
    KY2成绩排序
    python 获取list中元素的索引
    pandas 读取指定一列数据
    python 删除列表中的第一位元素
    python 时间戳
    python 除法保留小数点后两位
    python 读取excel表格的一列数据并去重
    python中获取Excel表格sheet页整页内容
    IDEA创建spring boot项目
    servlet一些问题
  • 原文地址:https://www.cnblogs.com/george92/p/8968599.html
Copyright © 2011-2022 走看看