#!/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()表示忽略空格
item_price = shopping_list[choice][1]
#輸出選擇商品的價格,這裏把(‘xxx’,xxxx)當成兩個元素,單獨取第二個輸出爲價格
print item_price
#判斷預算是否大於商品價格,若是大於就減去當前商品,打印輸出已經購買的商品,和剩餘的預算;不然提示從新輸入
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 have \033[1;32m %s \033[0m. \n" % budget
else:
print "Sorry, you can not afford to buy %s,try another!" %shopping_list[choice][0]
python