python 之模擬購物車
阿新 • • 發佈:2017-05-10
current 購物車 pri book you png lar 技術分享 style
#全部小代碼在此 product_list = [[‘Iphone7‘,5800], [‘Coffee‘,30], [‘疙瘩湯‘,10], [‘Python Book‘,99], [‘Bike‘,199], [‘ViVo X9‘,2499] ] shopping_cart = [] salary = int(input("input your salary:")) while True: menu= u‘‘‘ ------- Good Shopping --------- \033[32;1m 1. 輸入序號買商品進入購物車 2. 輸入q打印購物清單退出 \033[0m‘‘‘ print(menu) index = 0 for product in product_list: print(index,product) index +=1 choice = input(">>").strip() if choice.isdigit(): #判斷是否為數字 choice = int(choice) if choice >= 0 and choice < len(product_list):#商品存在 product = product_list[choice]#取到商品 if product[1] <= salary:#判斷能否買得起 shopping_cart.append(product)#加入購物車 salary -= product[1]#扣錢 print("added product "+ product[0] + "into shopping cart,your current balance"+ str(salary)) print("現在的余額為 %d 元" % salary) #print("jia ge shi " + product[haproxy.conf] + "yuan") else: print("買不起,窮逼!產品價格是"+str(product)[1]+"你還差錢"+str(product[1]-salary)+"錢") else: print("商品不存在") elif choice == "q": print("----已購買商品列表-----") print("id 商品 數量 單價 總價") for i in shopping_cart: print(i) print("您的余額為:",salary) print("------end------") break else: print("無此選項!")
python 之模擬購物車