Tag: End rod Ends Shopping cart budget number established choice COFF
Target Requirements:
1. User Input Shopping Budget
2, print the product list, by the user choice, budget enough to buy, not enough to prompt
3. Enter Q to exit the program
4, shopping end, show the purchase of things and balance
Realize:
1. Use the list to store goods and price information
2, set up empty list to store the goods purchased, each time the purchase is successful, append
3, if determine whether the number, whether it is Q, whether to enter the correct product number, while implementation loop, until the user input Q exit
1 Print("---This program is used to purchase budget items. ---")2 Print("---The user enters the budget, then selects the commodity, if the budget is sufficient, buys, joins the shopping cart, otherwise prompts cannot buy. ---" )3 Print("---Press Q to exit the purchase process. ---\ n")4 5 defprintproduct (list):6 forIdx,iteminchEnumerate (list):7 Print(Idx+1,":", item)8 9product=["IPhone 6500","Bike","Macbook 12000","Watch","Coffee","Book Bayi"]Tencart=[] One ABudget=input ("Please enter your purchase budget or press Q to exit:") - whilebudget!='Q': - ifStr (budget). IsDigit (): thebudget=Int (budget) - printproduct (product) -Choiceidx = input ("Please enter the item number you want to buy:") - whilechoiceidx!='Q': + ifchoiceidx.isdigit (): -choiceidx=Int (CHOICEIDX) + ifChoiceidxinchRange (1,len (product) +1): A ifInt (Product[choiceidx-1].split (" ") [1]) <Budget: at Print("Products "", Product[choiceidx-1].split (" ") [0],""Purchase Success") -Cart.append (Product[choiceidx-1].split (" ") [0]) -Budget-= Int (Product[choiceidx-1].split (" ") [1]) - Else: - Print("not enough budget to buy") - Else: in Print("Please enter the correct product number") - Else: to Print("Please enter the correct product number") + printproduct (product) -Choiceidx = input ("Please enter the item number you want to buy:") the Else: * Print("you have bought:") $ Print(CART)Panax Notoginseng Print("you have left:", Budget) - exit () the Else: + Print("The budget you entered is not a number, please enter it again. ") ABudget = input ("Please enter your shopping budget:") the Print("you have bought:") + Print(CART) - Print("you have left:", budget)
python--Simple Shopping Cart realization