购物车代码练习

1、老男孩购物车代码


2、老男孩购物车代码第二部分:





3、购物车程序练习

     

# Author: wang  cheng   hua   购物车程序代码编写

product_list =[
('apple',6000),
('ipad',3500),
('books',90),
('coff',30),
('watch',2000),
('shoes',500)
]
shopping_list = []

salary = input('input you salary : ')# 输入工资
if salary.isdigit():                 # 判断工资是否为数字
   salary = int (salary)
   while True :
      for index,item  in   enumerate (product_list):
         #print(product_list.index(item),item)
         print(index,item)           # 打印商品列表
      user_choice = input ('选择要买嘛?》》》:')  # 客户选择商品 
      if user_choice.isdigit():                     # 判断客户的选择商品代码是否是数字
          user_choice = int(user_choice)
          if user_choice < len(product_list) and user_choice >=0: #  判断客户的选择的商品代码是否在产品清单里 
             p_item = product_list[user_choice]                   # 提取客户的选择产品
             if p_item[1] <=salary :                              #判断客户是否买的起 
                shopping_list.append(p_item)                      #买的起的时候,将选择的产品放入购物车清单中
                salary -=p_item[1]                                # 扣除费用
                print('Added %s  into shopping cart,your current balance is 33[31;1m%s33[0m'%(p_item,salary) )
             else:
                print('33[41;m你的余额只剩[%s]啦,买不了啦33[0m'%(salary)) 
          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('you current balance :',salary)
      else:                                             # 客户输入的不是清单序列号 
         print('invalid option')

  

原文地址:https://www.cnblogs.com/wangchenghua/p/10876341.html