The 14th lesson of Python Learning--basic data exercise

Source: Internet
Author: User

Shopping Cart

Functional Requirements:

Require users to enter total assets, for example: 5000

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

Construct with a list to buy products, shopping carts

#!/usr/bin/env python#-*-coding:utf-8-*-a=["Mobile Phone","Computer","Camera","Flat Panel"]#Use list construction to buy products, shopping cartsmoney=0moneyadd=input ("Please enter total amount:")#Enter total assetsifmoneyadd.isdigit ():#determine if the user is entering a pure number    PassElse: Exit ("you're not typing in numbers.") Money=Int (moneyadd)#converts the amount of money entered by the user into an integer typePrint("your total amount of money:", Money,"Yuan")Print("items available for purchase are:") Goods=[    {"name":"Mobile Phone","Unit Price": 1000},    {"name":"Computer","Unit Price": 5000},    {"name":"Camera","Unit Price": 3000},    {"name":"Flat Panel","Unit Price": 2000}]goodsadd=[]#to buy goods forIinchGoods:#display the product and price by looping through the keys of the dictionary    Print(i["name"],i["Unit Price"],"Yuan") Detailed="" whileTrue:detailedadd=input ("Enter M settlement \ Please enter the name of the product you want to buy:")#require the user to enter the name of the product to be purchased    ifdetailedadd=="M":         Break    Else:         forIinchGoods:ifi["name"]==detailedadd:detailed=i["name"]            Else:                Pass        ifdetailed==Detailedadd:Pass        Else:             forIinchGoods:ifi["name"]==Detailedadd:Goodsadd.append (i)Print("you have selected:", Goodsadd) Zongjia=0 forIinchGoodsadd:danjia=i["Unit Price"] Zongjia+=DanjiaPrint("now settlement total:", Zongjia,"Yuan")ifmoney>=Zongjia:Print("Purchase Success")Else:    Print("with insufficient amount")

The 14th lesson of Python Learning--basic data exercise

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.