Python dictionary implementation Simple shopping Cart

Source: Internet
Author: User

#-*-coding:utf-8-*-
#总金额
Asset_all=0
i1=Input' Please enter total assets: ')
Asset_all=int (i1)
#商品列表
goods=[
{' Name ':Computer,' Price ':' 3999 '},
{' Name ':Mouse,' Price ':' 129 '},
{' Name ':Keyboard,' Price ':' 69 '},
{' Name ':' iphone ',' Price ':' 5388 '}
]
For IIn Goods:
Print (i[' Name '], i[' Price '])

car_dict={}#定义空字典存放购买的商品
# {
# ' name ': {' num ': ', ' price ': '} storage format
# }

While1:
I2=Input' Please select Product (y/y settlement): ')#选择购买的商品
If i2.lower () = =' Y ':
Break
For itemIn Goods:#循环商品列表, Item gets to product
If item[' Name ']==i2:#判断是否与输入一致
name=item[' Name ']#重新赋值
If nameIn Car_dict.keys ():#判断购买的商品在购物车中是否存在
car_dict[name][' Num ']= car_dict[name][' Num ']+1#如果存在, the number is +1.
Else
car_dict[name]={' Num ':1,' Single_price ': item[' Price ']}#商品在购物车中不存在就按照格式添加进来
Print (car_dict)#g购物车清单

#跳出循环去结算
All_price= 0 #购物总价
for k,v in car_dict.items ():
N=v[ ' single_price ']
M=v[ num ']
all_sum=m*n
All_price=all_price+int (all_sum)
if all_price<=asset_all:
print ( ' buy success ')
else:
print (



















Python dictionary implements a simple 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.