#!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 '輸入有誤!,請從新輸入。'