Python购物车的实现课程
需求:
1.用户输入工资收入
2.打印商品列表
3.用户选择商品,不断的加入购物车
4.检测用户余额,直接捐款,不足提示余额不足
5.允许主动退出,退出时,打印已购商品列表
重点方法:
打印列表下标的方法:
a=['alex','sys','root','admin']
>>> for index,i in enumerate(a):
... print(index,i)
0 alex
1 sys
2 root
3 admin
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/env python3 2 # -*-conding:utf-8-** 3 # __Author__:'liudong' 4 salary=input("Input your salary:") 5 if salary.isdigit(): 6 salary=int(salary) 7 else: 8 exit("Invalid data type...") 9 welcom_msg='Welcome to Shopping mall'.center(50,'-') 10 print(welcom_msg) 11 exit_flag=False 12 product_list = [ 13 ('Iphone',5000), 14 ('Mac Air',8000), 15 ('Mac Pro',9000), 16 ('XiaoMi',20), 17 ('Coffe',30), 18 ('Bike',800), 19 ('Cloth',200) 20 ] 21 shop_car = [] 22 while not exit_flag: 23 #for product_item in product_list: 24 # p_name,p_price = product_item #可选的写法 25 print("Products list".center(50,'-')) 26 # for p_name,p_price in product_list: 27 # print(p_name,p_price) #由于此方法后面打印下标时,会变成2个无组,所以用下面的方法打印 28 for item in enumerate(product_list): 29 index=item[0] 30 p_name=item[1][0] 31 p_price=item[1][1] 32 print(index,'.',p_name,p_price) 33 user_choice = input('[q=quit,c=check]What do you want to buy?:') 34 if user_choice.isdigit(): #肯定是选商品 35 user_choice = int(user_choice) 36 if user_choice < len(product_list): 37 p_item = product_list[user_choice] 38 if p_item[1] <= salary: #买的起 39 shop_car.append(p_item) #放入购物车 40 salary -= p_item[1] #扣钱 41 print('Added [%s] into your shop car,your current balance is \033[31;1m[%s]\033[0m' % 42 (p_item,salary)) #字体加颜色 43 else: 44 print('Your balance is [%s],cannot afford this product..' %salary) 45 else: 46 if user_choice == 'q' or user_choice == 'quit': 47 print('purchased products as blew:'.center(40,'*')) 48 for item in shop_car: 49 print(item) 50 print('END'.center(40,'*')) 51 print('Your balance is [%s]' %salary) 52 print('bye.') 53 exit_flag = True 54 elif user_choice == 'c' or user_choice == 'check': 55 print('purchased products as blew:'.center(40, '*')) 56 for item in shop_car: 57 print(item) 58 print('Your balance is \033[41;1m[%s]\033[0m' % salary) #背景加颜色
编程的思维还要加强。。。。。
“我们知道,世界上存在着已知的已知事物,也就是说有些事情我们知道自己知道,而我们也知道世上存在着被人所知的不明事物,这就是说有些事情我们知道自己不知道。同时,世上还存在着我们不知道的不明事物,也就是说我们不知道自己不知道。”