Python Shopping Cart Merchant part code

Source: Internet
Author: User
Knowledge Points: File write operations, functions, function recursion

#! C:\Program files\python35/bin#-*-conding:utf-8-*-# author:frank# definition Product List product_list = {' Pen ': 2.5, ' NoteBook ': 3. 3, ' eraser ': 1.0}MESG = ' 1.Change goods ' price2. ADD new Goods3.quitplese input which do want: "Def print_list (): For key in Product_list:print (key, Produ Ct_list[key]) def change_price (): name = input ("Please input the goods name:") if name in PRODUCT_LIST:NEW_PR  Ice = input ("Please input new price:") product_list[name] = New_price print ("\033[31;0m[%s]\033[0m been change to \033[32;0m%s\033[1m "% (name, new_price)) print_list () Else:ok = input (" The goods not in P Roduct list, do you want to add: ") if OK in (' Y ', ' ye ', ' yes '): Add_goods () If OK in (' n ', ' no ', ' non E '): Exit (Record_to_text ()) def add_goods (): name = input ("Please input the Add goods name:") if name in P Roduct_list:print ("The goods had exist, please re_input:") add_goods () ElsE:price = input ("Please input the goods price:") product_list[name] = Price print ("Add \033[31;0m[%s ]\033[1m to list already "% name" Print_list () def record_to_text (): With open ("Product", ' w+ ', encoding= ' utf-8 ') As F:for goods in Product_list:print (Goods,product_list[goods], file=f) print_list () while True:user         _input = input (MESG) if user_input = = ' 1 ': Change_price () elif user_input = ' 2 ': Add_goods () Else: User_input = = ' 3 ' Print ("Your current produce list as below:") Print_list () exit (Record_to_te XT ())

Test:

NoteBook 3.3eraser 1.0Pen 2.51.Change goods ' Price2.  Add new Goods3.quitplese input which do want:p lease input the Add goods name:appleplease input the goods price:5.0add [Apple] into list alreadyapple 5.0NoteBook 3.3eraser 1.0Pen 2.51.Change goods ' Price2.  ADD new Goods3.quitplese input which do want:p lease input the goods name:penplease input new Price:2.3[pen] price have been change to 2.3Apple 5.0NoteBook 3.3eraser 1.0Pen 2.31.Change goods ' Price2. ADD new Goods3.quitplese input which do want:p lease input the goods name:bookthe goods not in the product list, does you WA NT to Add:no

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.