Using Python to implement a simple shopping cart

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-

Shopping_list = [
(' Iphone ', 5800),
(' Bike ', 800),
(' book ', 45),
(' Coffee ', 35),
(' Solo 2 Beats ', 1590),
(' MX4 ', 1999),
]
#定义一个商品列表

Budget = Int (raw_input ("Please input your budget:"). Strip ())
#输入预算

Buy_list = []
#定义购物车列表

While True:
For I in Shopping_list:
Print Shopping_list.index (i), I
#循环打印出商品的index和商品名称
Choice = Int (raw_input ("Please input your choice:"). Strip ())
#输入选择的商品, strip () indicates that spaces are ignored
Item_price = shopping_list[choice][1]
#输出选择商品的价格, this place (' xxx ', xxxx) as two elements, separately take the second output for the price

Print Item_price
#判断预算是否大于商品价格, if it is greater than the current item is subtracted, the printed output has been purchased, and the remaining budget; otherwise prompt for re-entry
If budget >= Item_price:
Budget-= Item_price
Buy_list.append (Shopping_list[choice])
Print "Added \033[1;33m%s \033[0m into shopping list."% shopping_list[choice][0]
Print "You just only has \033[1;32m%s \033[0m. \ n "% budget
Else
Print "Sorry, you can not afford to buy%s,try another!"%shopping_list[choice][0]



This article is from the "Rabbit" blog, make sure to keep this source http://xiajie.blog.51cto.com/6044823/1721079

Using Python to implement 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.