#!/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