Python basic data type implementation Shopping Cart

Source: Internet
Author: User

Functional Requirements:

    • Require users to enter total assets, for example: 2000
    • Display the list of items, let the user select the item according to the serial number, add the shopping cart
    • Purchase, if the total amount of goods is greater than the total assets, indicating that the account balance is insufficient, otherwise, the purchase succeeds.
    • Add: Can recharge, a product to remove the shopping cart
123456 goods =[    {"name""电脑""price"1999},    {"name""鼠标""price"10},    {"name""游艇""price"20},    {"name""美女""price"998},]
Goods = [    {"name":"Computer"," Price": 1999},    {"name":"Mouse"," Price": 10},    {"name":"Yacht"," Price": 20},    {"name":"Beauty"," Price": 998},]money=int (Input ("Please enter your total amount:")) Cart={}#define an empty shopping cart#Product List forIinchGoods:Print(i["name"],i[" Price"]) whileTrue:i2=input ("Please select goods, y/y settlement;")    #exit the Loop if settlement is selected    ifI2.lower () = ="y":         Break;  forIteminchGoods:ifitem["name"]==I2:name=item["name"]            #if the cart already has the item, the number +1, if the beauty, then the new dictionary is used to store the product information            ifNameinchCart.keys (): cart[name]["Num"]+=1; Else: Cart[name]={"Num": 1,'Sigle_price': item[" Price"]}    Print(CART)#Checkoutall_prices=0 forKvinchCart.items (): N=v['Sigle_price'] M=v['Num'] All_sum=n*m; All_prices+=all_sum;ifAll_prices>Money :Print("Insufficient Balance")Else:    Print("consumption:", all_prices)

The following implementation method has a small problem, the solution has not been found for the time being, a friend who knows is welcome to correct me!

"""Functional Requirements: Require the user to enter total assets, for example: 2000 display a list of items, 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 success. Add: Can recharge, a product to remove the shopping cart""""""1. Create an empty shopping cart List 2. Require the user to enter an amount of assets 3. Enter a list of items 4. The user selects the item, adds the shopping cart 5. Settlement, if the commodity price is greater than the user's total assets prompt balance is insufficient, ask whether to delete the item 6. Cash out, prompt for successful purchase, show balance and exit """Goods= [    {"name":"Computer"," Price": 1999},    {"name":"Mouse"," Price": 10},    {"name":"Yacht"," Price": 20},    {"name":"Beauty"," Price": 998},]cart={"good_list":[],"price_list":[]}#user Input Account amountMoney=int (Input ("Please enter your account amount:"))#Print product InformationPrint("Product List:") whileTrue: forKvinchEnumerate (goods,1):         forIinchRange (len (goods)):ifk-1==I:Print(k,goods[i]["name"],goods[i][" Price"])    #Users Select ProductsProducts=int (Input ("Please select a product you like:"))    Print("The products you choose are:", goods[products-1]["name"],"Cost:", goods[products-1][" Price"])    #calculate the balance after selecting a productcur_assets=money-goods[products-1][" Price"]    ifCur_assets>0:cart["good_list"].append (goods[products-1]["name"])#If the money is sufficient, add the item to the shopping cart and settlecart["price_list"].append (goods[products-1][" Price"])        Print("your current account balance is:", Cur_assets,"¥") Choose=input ("to continue shopping, please press ' C '; check out, press ' o '; display information, press ' l '; exit, please press ' Q '")        #View product information to add to your cart        ifchoose=='L':             forProd,valinchEnumerate (cart["good_list"],1):                Print(Prod,val) Break        elifchoose=='Q':            Print("Kaejinn")             Break; elifchoose=='C':            Continue        elifchoose=='o': Total=sum (cart["price_list"])            ifTotal>Money :Print("Insufficient balance! ")            Else:                Print("Thank you, this time cost:", Total,'¥',"Balance:", Money-total,"¥")                 Break; Else:            Print("error, please reenter:")    Else:        Print("Sorry, your account balance is not enough! ")         Break

Python basic data type implementation 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.