#!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('computer',6000), ('girl_friend',2000), ('boy_friend',3000)] shop_lists = [] for i,v in enumerate(product_lists): #python的内置函数,在字典上是枚举,列举的意思,可以同事获得索引和值 print (i,v) while True: money = raw_input('请输入你的钱数:') if money.isdigit(): money = int(money) while True: choise = raw_input('请输入商品序列号,q退出:') if choise.isdigit(): choise = int(choise) if choise >= 0 and choise < len(product_lists): item = product_lists[choise] if money >= item[1]: shop_lists.append(item) money-=item[1] print '%s 已经加入购物车,还剩 %d 元'%(item,money) else: print '钱不够啊!' else: print '没有这个商品!' elif choise=='q': print '已经退出系统,你一共买了这些商品:' for i in shop_lists: print i print '还剩%d元'%money qw = 1 break else: print '输入无效!' if qw == 1: break else: print '输入有误!,请从新输入。'