Python入门第六天_练习
购物车小程序:
1 #! usr/bin/env pyhton 2 # -*- coding:utf-8 -*- 3 4 5 salary=input("请输入你的购物预算:") 6 7 car_list=[] #用户选择商品存放购物列表 8 9 goods=[ 10 ('iphone6s',5800), 11 ('mac book',9000), 12 ('coffee',32), 13 ('python book',80), 14 ('bicyle',1500) 15 ] 16 17 #把物品展示出来 18 for i in goods: 19 print(i) 20 21 22 while True: 23 select_goods=input("请选择商品(Y/y结算):") 24 if select_goods.lower() == 'y': 25 break 26 for j in goods: 27 if j[0] == select_goods: 28 car_list.append(j) 29 30 #结算 31 32 all_price = 0 33 for items in car_list: 34 all_price+=items[1] 35 36 print(salary,all_price) #输出预算和购物总价 37 if int(all_price) > int(salary): 38 print("您的预算不足。请备好充足的资金。") 39 else: 40 print("购买成功!欢迎下次光临")
结果:
================= RESTART: F:\python全栈开发(从入门到放弃)\购物车的小程序.py ================= 请输入你的购物预算:50000 ('iphone6s', 5800) ('mac book', 9000) ('coffee', 32) ('python book', 80) ('bicyle', 1500) 请选择商品(Y/y结算):iphone6s 请选择商品(Y/y结算):mac book 请选择商品(Y/y结算):coffee 请选择商品(Y/y结算):bicyle 请选择商品(Y/y结算):y 50000 16332 购买成功!欢迎下次光临 >>> ================= RESTART: F:\python全栈开发(从入门到放弃)\购物车的小程序.py ================= 请输入你的购物预算:6000 ('iphone6s', 5800) ('mac book', 9000) ('coffee', 32) ('python book', 80) ('bicyle', 1500) 请选择商品(Y/y结算):coffee 请选择商品(Y/y结算):mac book 请选择商品(Y/y结算):y 6000 9032 您的预算不足。请备好充足的资金。 >>>
购物车:对输入的数字进行判断
1 #! usr/bin/env pyhton 2 # -*- coding:utf-8 -*- 3 4 flag=True 5 flag_shopping = True 6 while flag: 7 for i in range(3): 8 salary=input("请输入你的购物预算:") 9 if salary.isdigit(): 10 print("输入正确,祝您购物愉快!!") 11 flag=False 12 break 13 else: 14 print('您输入的预算数字格式不对,请重新输入') 15 16 else: 17 print("是否还要继续??") 18 flag_ensure=input("请输入 是/否 ?") 19 if flag_ensure=='是': 20 flag=True 21 else: 22 flag=False 23 #购物 24 car_list=[] #用户选择商品存放购物列表 25 goods=[ 26 ('iphone6s',5800), 27 ('mac book',9000), 28 ('coffee',32), 29 ('python book',80), 30 ('bicyle',1500) 31 ] 32 for j in goods:#把物品展示出来 33 print(j) 34 35 while True: 36 select_goods=input("请选择商品(Y/y结算):") 37 if select_goods.lower() == 'y': 38 break 39 for good_s in goods: 40 if good_s[0] == select_goods: 41 car_list.append(good_s) 42 43 all_price = 0 # 结算 44 for items in car_list: 45 all_price += items[1] 46 47 print(salary, all_price) # 输出预算和购物总价 48 if int(all_price) > int(salary): 49 print("您的预算不足。请备好充足的资金。") 50 else: 51 print("购买成功!欢迎下次光临")
购物车:增加判断是否继续购物
1 #! usr/bin/env pyhton 2 # -*- coding:utf-8 -*- 3 4 flag=True 5 flag_shopping = True 6 while flag_shopping: 7 while flag: 8 for i in range(3): 9 salary=input("请输入你的购物预算:") 10 if salary.isdigit(): 11 print("输入正确,祝您购物愉快!!") 12 flag=False 13 break 14 else: 15 print('您输入的预算数字格式不对,请重新输入') 16 17 else: 18 print("是否还要继续??") 19 flag_ensure=input("请输入 是/否 ?") 20 if flag_ensure=='是': 21 flag=True 22 else: 23 flag=False 24 #购物 25 car_list=[] #用户选择商品存放购物列表 26 goods=[ 27 ('iphone6s',5800), 28 ('mac book',9000), 29 ('coffee',32), 30 ('python book',80), 31 ('bicyle',1500) 32 ] 33 for j in goods:#把物品展示出来 34 print(j) 35 36 while True: 37 select_goods=input("请选择商品(Y/y结算):") 38 if select_goods.lower() == 'y': 39 break 40 for good_s in goods: 41 if good_s[0] == select_goods: 42 car_list.append(good_s) 43 44 all_price = 0 # 结算 45 for items in car_list: 46 all_price += items[1] 47 48 print(salary, all_price) # 输出预算和购物总价 49 if int(all_price) > int(salary): 50 print("您的预算不足。请备好充足的资金。") 51 shopping_判断=input("是否需要重新输入预算,开始购物?(y/n)") 52 if shopping_判断.lower()=='n': 53 flag_shopping=False 54 else: 55 flag=True 56 else: 57 print("购买成功!欢迎下次光临") 58 shopping_判断 = input("是否需要继续购物?(y/n)") 59 if shopping_判断.lower() == 'n': 60 flag_shopping = False 61 else: 62 flag=True
购物车: 对判断的输入值进行判断
只能是y/n两个字符。
1 #! usr/bin/env pyhton 2 # -*- coding:utf-8 -*- 3 4 flag=True 5 flag_shopping = True 6 yes_or_no=('y','n')#用元组,不允许更改元组中的值 7 while flag_shopping: 8 while flag: 9 for i in range(3): 10 salary=input("请输入你的购物预算:") 11 if salary.isdigit(): 12 print("输入正确,祝您购物愉快!!") 13 flag=False 14 break 15 else: 16 print('您输入的预算数字格式不对,请重新输入') 17 18 else: 19 print("是否还要继续??") 20 flag_ensure=input("请输入 是/否 ?") 21 if flag_ensure=='是': 22 flag=True 23 else: 24 flag=False 25 #购物 26 car_list=[] #用户选择商品存放购物列表 27 goods=[ 28 ('iphone6s',5800), 29 ('mac book',9000), 30 ('coffee',32), 31 ('python book',80), 32 ('bicyle',1500) 33 ] 34 for j in goods:#把物品展示出来 35 print(j) 36 37 while True: 38 select_goods=input("请选择商品(Y/y结算):") 39 if select_goods.lower() == 'y': 40 break 41 for good_s in goods: 42 if good_s[0] == select_goods: 43 car_list.append(good_s) 44 45 all_price = 0 # 结算 46 for items in car_list: 47 all_price += items[1] 48 49 print(salary, all_price) # 输出预算和购物总价 50 shopping_判断='' 51 if int(all_price) > int(salary): 52 print("您的预算不足。请备好充足的资金。") 53 shopping_判断=input("是否需要重新输入预算,开始购物?(y/n)") 54 if shopping_判断.lower() in yes_or_no and shopping_判断.lower()=='n': 55 flag_shopping=False 56 else: 57 if shopping_判断.lower() in yes_or_no and shopping_判断.lower()=='y': 58 flag=True 59 else: 60 print("您的输入不正确。只能输入(y/n)两个字母") 61 break 62 else: 63 print("购买成功!欢迎下次光临") 64 shopping_判断 = input("是否需要继续购物?(y/n)") 65 if shopping_判断.lower() in yes_or_no and shopping_判断.lower()=='n': 66 flag_shopping = False 67 else: 68 if shopping_判断.lower() in yes_or_no and shopping_判断.lower()=='y': 69 flag=True 70 else: 71 print("您的输入不正确。只能输入(y/n)两个字母") 72 break
购物车 。。。。
1 # encoding:utf-8 2 product_list=[ 3 ('iphone6s',5800), 4 ('mac book',9000), 5 ('coffee',32), 6 ('python book',80), 7 ('bicyle',1500) 8 ] 9 saving=input("please input your saving:") 10 shopping_car=[] 11 if saving.isdigit(): 12 saving=int(saving) 13 while True: 14 15 for i,j in enumerate(product_list,1): 16 print(i,'>>>',j) 17 choice=input('choice your product number[quit:q]') 18 if choice.isdigit(): 19 choice=int(choice) 20 if choice>0 and choice<= len(product_list): 21 p_item=product_list[choice-1] 22 if p_item[1]<saving: 23 saving-=p_item[1] 24 shopping_car.append(p_item) 25 else: 26 print('余额不足,还剩%s'%saving) 27 print(p_item) 28 else: 29 print('number is out inside') 30 elif choice.lower()=='q': 31 print('----------已经购买如下商品-------------') 32 print('编码','商品名称','商品价格','数量',sep='\t') 33 y_sum=[] 34 x = 1 35 36 for y in shopping_car: 37 38 if y not in y_sum: 39 40 print(x,y[0],y[1],shopping_car.count(y),sep='\t') 41 y_sum.append(y) 42 x=x+1 43 print('剩余金额%s'%saving) 44 break 45 else: 46 print("invalid input")