实现购物车:
#!/usr/bin/env python
#coding:utf-8
__author__ = "lvah"
'''
@author:wangjiazhuo
@file:购物车.py
@contact:pucca@163.com
@time:7/12/176:23 PM
@desc:
'''
salary = raw_input('input your salary:')
if salary.isdigit():
salary = int(salary)
shop_car = []
mall = [
('Ipone',8000),
('Book',200),
('Bike',1000)
]
for item in enumerate(mall):
num = item[0]
name = item[1][0]
price = item[1][1]
print num, name, price
while True:
choice = raw_input('choose you want to buy: q to exit')
if choice.isdigit():
choice = int(choice)
if choice <= len(mall)-1:
p_item = mall[choice]
if salary < p_item[1]:
print '余额不足,请购买其他商品或退出'
elif salary > 0:
shop_car.append(p_item)
salary -= p_item[1]
print '\n你买到的商品有:',
for item in shop_car:
print item
print '你的余额为:%d'%salary
else:
pass
else:
print '请输入已有的商品编号'
elif choice == 'q':
print '\n你买到的商品有:'
for item in shop_car:
print item
print '你的余额为:%d'%salary
exit(0)
else:
print '请重新选择'
执行结果: