1. Requirements:
1 After you start the program, let the user enter the payroll, and then print the list of items 2 allow users to purchase items based on their product number 3 4 can exit at any time, when you exit, print purchased goods and balances
2. Code:
1commodity = [[1,'Iphone7', 5888],[2,'Mac Pro', 12888],[3,'ipad 4', 2888],[4,' Book', 66],[5,'Bike', 888]]2shoppingcart=[]3 4 Print("Product List:")5 forIinchCommodity:6 Print(i)7 8balance = Int (input ("\ n Please enter your shopping budget:"))9 Ten while1: OneCommodityid = input ("Please enter the item ID to be purchased:") A ifCommodityid = ='Q' orCommodityid = ='Q': - Print("----------------------------") - Print("your current balance is:"+str (balance)) the Print("you have the following items in your shopping cart:") - forIinchShoppingCart: - Print(i) - Print("----------------------------") + Break - + forJinchCommodity: A ifJ[0] = =Int (Commodityid): at ifBalance > J[2]: -Balance = balance-j[2] - Shoppingcart.append (j) - Print("----------------------------") - Print("added success!!! ") - Print("your current balance is:"+str (balance)) in Print("----------------------------") - Break to Else: + Print("----------------------------") - Print('Please select another item, your current balance is:'+str (balance)) the Print('Enter Q to exit the shopping') * Print("----------------------------") $ BreakPanax Notoginseng Else: - Print('If you do not have this item, please enter it again. ') the Print("Product List:") + forIinchCommodity: A Print(i)
3. Operation Result:
1 Product List:2[1,'Iphone7', 5888]3[2,'Mac Pro', 12888]4[3,'ipad 4', 2888]5[4,' Book', 66]6[5,'Bike', 888]7 8Please enter your shopping budget: 111119Please enter the item you want to buy id:1Ten---------------------------- One added success!!! AYour current balance is: 5223 ----------------------------- -Please enter the item you want to buy id:1 the---------------------------- -Please select another product, your current balance is: 5223 - Enter Q to exit the shopping ----------------------------- +Please enter the item you want to buy Id:2 ----------------------------- +Please select another product, your current balance is: 5223 A Enter Q to exit the shopping at---------------------------- -Please enter the item you want to buy Id:3 ----------------------------- - added success!!! -Your current balance is: 2335 ----------------------------- inPlease enter the item you want to buy Id:4 ----------------------------- to added success!!! +Your current balance is: 2269 ----------------------------- thePlease enter the item you want to buy Id:5 *---------------------------- $ added success!!! Panax NotoginsengYour current balance is: 1381 ----------------------------- thePlease enter the item you want to buy Id:6 + If you do not have this item, please enter it again. A Product List: the[1,'Iphone7', 5888] +[2,'Mac Pro', 12888] -[3,'ipad 4', 2888] $[4,' Book', 66] $[5,'Bike', 888] -Please enter the item you want to buy Id:2 ----------------------------- thePlease select another product, your current balance is: 1381 - Enter Q to exit the shoppingWuyi---------------------------- thePlease enter the item you want to buy Id:4 ----------------------------- Wu added success!!! -Your current balance is: 1315 About---------------------------- $ Please enter the item you want to buy Id:q ----------------------------- -Your current balance is: 1315 - you have the following items in your shopping cart: A[1,'Iphone7', 5888] +[3,'ipad 4', 2888] the[4,' Book', 66] -[5,'Bike', 888] $[4,' Book', 66] the----------------------------
Python Notes 2 Shopping Cart Program