python 简单购物车小程序
salary=5000 goods_list=[['iphone6s',5800],['mac book',9000],['coffee',32],['python book',50],['bicycle',1500]] print("Welcome to our store,the following is our goods _list") shoping_cart=[]#购物车,进入购物车的商品是已购商品,如果要做未购商品的购物车,只需要加个变量salary_tmp #打印商品列表 for i,j in enumerate(goods_list,1): # print("%d. %-12s %d"%(i,j,k)) print("%d. %-12s %d"%(i,j[0],j[1])) while True: num=input("Please input the goods number you want to buy:") #对输入进行检查,输入是quit退出,是商品号进行处理,是其他的报错 if num.isdigit(): num=int(num)-1 elif num=="quit": break else: print("please input quit or the goods number") continue #获取商品价格和商品名称 goods_price=goods_list[num][1] goods=goods_list[num][0] #如果余额不足则无法购买,余额足够则让余额减少 if goods_price>salary : print("Your money is not enough",salary-goods_price) else: print("the %s has already put into your shoping cart,your recent money: %d"%(goods,salary-goods_price)) shoping_cart.append([goods,goods_price]) salary -= goods_price #已购商品提示 print("You have already bought %d goods"%(len(shoping_cart))) for i in range(len(shoping_cart)): print("%d. %-12s %d"%(i+1,shoping_cart[i][0],shoping_cart[i][1])) print("Your recent money:%d\nLooking forward to your next visit"%salary)
Welcome to our store,the following is our goods _list 1. iphone6s 5800 2. mac book 9000 3. coffee 32 4. python book 50 5. bicycle 1500 Please input the goods number you want to buy:1 Your money is not enough -800 Please input the goods number you want to buy:2 Your money is not enough -4000 Please input the goods number you want to buy:3 the coffee has already put into your shoping cart,your recent money: 4968 Please input the goods number you want to buy:4 the python book has already put into your shoping cart,your recent money: 4918 Please input the goods number you want to buy:5 the bicycle has already put into your shoping cart,your recent money: 3418 Please input the goods number you want to buy:quit You have already bought 3 goods 1. coffee 32 2. python book 50 3. bicycle 1500 Your recent money:3418 Looking forward to your next visit
随便用