自学练习,记录下来,如有不对的地方,欢迎各位大神指出来!
程序:购物车程序
需求:
- 启动程序后,让用户输入预算,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
代码:

1 #cherry_cui 2 3 mall_list = [ 4 ('Bicycle',800), 5 ('computer',4800), 6 ('pizza',50), 7 ('skirt',200), 8 ('shoes',1200), 9 ('iphone6',3200), 10 ('watch',2700) 11 ] 12 shopping_cart =[] 13 budget = input("Input your budget:") #输入购物预算 14 if budget.isdigit(): #判断budget 是否只由数字组成。 15 budget = int(budget) #转换成int类型 16 17 while True: 18 for num,item in enumerate(mall_list): # enumerate()获取mall_list的下标 19 print(num,item) 20 21 choice = input("which goods you want(enter the number):") #输入需要购买的商品编号 22 if choice.isdigit(): #判断choice 是否只由数字组成。 23 choice = int(choice) #转换成int类型 24 25 if choice < len(mall_list) and choice >= 0: #len() 方法返回mall_list列表元素个数 26 u_item = mall_list[choice] 27 if u_item[1]<=budget: 28 budget-=u_item[1] 29 shopping_cart.append(u_item) #把已购买的商品加入购物车shopping_cart 30 print("%s had added to your shopping list,your current balance is %s !" %(u_item[0],budget)) 31 32 else: 33 print("Your current balance is %s ,not enough to buy %s ,choose again!" %(budget,u_item[0])) 34 35 else: 36 print("Product code %s is not exist,choose again" %choice) 37 38 39 elif choice == 'q': 40 print("-----shopping_cart-----") 41 for c in shopping_cart: 42 print(c) 43 44 print("Your current balance is %s" %budget) 45 exit() 46 47 48 else: 49 print("Input error!") 50 exit() 51 52 53 else: 54 print("Input error!")
结果:
正常流程:

Input your budget:10000 ('Bicycle', 800) ('computer', 4800) ('pizza', 50) ('skirt', 200) ('shoes', 1200) ('iphone6', 3200) ('watch', 2700) which goods you want(enter the number):1 computer had added to your shopping list,your current balance is 5200 ! ('Bicycle', 800) ('computer', 4800) ('pizza', 50) ('skirt', 200) ('shoes', 1200) ('iphone6', 3200) ('watch', 2700) which goods you want(enter the number):5 iphone6 had added to your shopping list,your current balance is 2000 ! ('Bicycle', 800) ('computer', 4800) ('pizza', 50) ('skirt', 200) ('shoes', 1200) ('iphone6', 3200) ('watch', 2700) which goods you want(enter the number):6 Your current balance is 2000 ,not enough to buy watch ,choose again! #余额不足 ('Bicycle', 800) ('computer', 4800) ('pizza', 50) ('skirt', 200) ('shoes', 1200) ('iphone6', 3200) ('watch', 2700) which goods you want(enter the number):2 pizza had added to your shopping list,your current balance is 1950 ! ('Bicycle', 800) ('computer', 4800) ('pizza', 50) ('skirt', 200) ('shoes', 1200) ('iphone6', 3200) ('watch', 2700) which goods you want(enter the number):q #退出程序 -----shopping_cart----- ('computer', 4800) ('iphone6', 3200) ('pizza', 50) Your current balance is 1950 Process finished with exit code 0
异常一:输入的预算不为数字
异常二:输入的商品编号大于存在的商品号 / 输入的商品编号不为数字

Input your budget:1000 0 ('Bicycle', 800) 1 ('computer', 4800) 2 ('pizza', 50) 3 ('skirt', 200) 4 ('shoes', 1200) 5 ('iphone6', 3200) 6 ('watch', 2700) which goods you want(enter the number):9 Product code 9 is not exist,choose again 0 ('Bicycle', 800) 1 ('computer', 4800) 2 ('pizza', 50) 3 ('skirt', 200) 4 ('shoes', 1200) 5 ('iphone6', 3200) 6 ('watch', 2700) which goods you want(enter the number):dsa Input error!
新学习的函数:
1. isdigit() 方法检测字符串是否只由数字组成
2. enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标
3. len() 方法返回对象(字符、列表、元组等)长度或项目个数。
其中,获取商品下标有两种方法
1.用enumerate()函数
2.list名称.index(数值) 就能拿到对应数值的索引(下标)
for item in mall_list: print(mall_list.index(item), item)