python--Simple Shopping Cart realization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.