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