Python Shopping Cart

Source: Internet
Author: User

#! /usr/bin/env python#-*-coding:utf-8-*-#-*-author:leon-*-"" "Functional Requirements: Require the user to enter total assets, for example: 2000 Display the product list, let the user select items according to the serial number, add shopping cart purchase, If the total amount of goods is greater than the total assets, the account balance is insufficient, otherwise, the purchase succeeds. Add: Can recharge, a product removal cart "" "# code is implemented as follows: goods = [{" Name ":" Computer "," Price ": 1999}, {" name ":" Mouse "," Price ": ten}, {" name ":" Yacht "," Price ": 2 0}, {"name": "Beauty", "price": 998},]cart = []while True:try:assets_input = input ("Please enter total asset amount:") assets = Int (assets_input) p Rint ("Total assets:", assets) break Except:print ("Input error ... Please re-enter! ") While True:for k,v in Enumerate (goods,1): print (k,v[' name '],v[' price ') TRY:INP = input ("Please enter the serial number of the item you want to buy:") inp_num = Int ( INP) Pass # To determine input correctness if inp_num< len (goods) +1:pass else:print ("Input ordinal error ... Please re-enter! ") Continue inp_key = goods[inp_num-1] Print ("************************ Shopping Cart Information *************************") Print (inp_key[' Name '],inp_key[' price '], "added to cart!") Print ("Items in the shopping cart as follows:") Cart.append (inp_key) expense = 0 for Inp_key,inp_value in Enumerate (cart,1): Print (Inp_key, "[", INP _value[' name '],inp_value[' price '], "[]") p = inp_value[' price '] ExpenSE = int (expense + p) If assets > Expense:shengyu = assets-expense print ("Also remaining:", Shengyu) print ("Submit purchase Please enter: y\n continue to purchase please enter: G \ n Empty cart input: c\n exit shopping Please enter: b\n update shopping Cart Input: w ") Print (" ******************************************************** ") userchoose = Input ("Please submit select:") if userchoose = = "Y": Count = Len (cart) print ("Quantity of goods purchased:", count) print ("Total Amount:", Shengyu) break Elif Userchoo Se = = "G": Continue elif Userchoose = = "C": Del cart[:] Print ("Emptied cart, item List display:", cart) elif userchoose = "W": Print ("****** Print ("Shopping cart list below:") for Inp_key,inp_value in Enumerate (cart,1): Print (Inp_key, "[", inp_value[' name '],inp_value[' price '], "]") shopupdate= input ("Please select the item number to be removed:") update_num = Int ( shopupdate) Update_numkey =update_num-1 cart.remove (Cart[update_numkey]) print (the "Shopping cart update list is as follows:") for Inp_key,inp_value in Enumerate (cart,1): Print (Inp_key, "[", inp_value[' name '],inp_value[' price '], "]") Count = Len (cart) print ("Number of items purchased:", count) p = inp_value[' price '] Shengyu = Assets-int (p) Print ("Still remaining: ", Shengyu) print (" ******************************************************** ") elif Userchoose==none or userchoose!= ' y ' or userchoose!= ' C ' or userchoose!= ' g ' or userchoose!= ' W ': Print ("Enter illegal value, reenter ...") Continue else: Userchoose = = "B" Break Else:print ("Money is not enough, please recharge:") Assets_chongzhi = input ("Please enter the Recharge amount:") Assets = assets + INT (Assets_chongzhi) Print ("Top up successfully, total assets:", assets) print ("********************************************************") Except:print (" Input ordinal error ... Please re-enter! ")

PS: Long time no write, practice practiced hand, level too wife ... No, haha.

Python Shopping Cart

Related Article

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.