1 #!/usr/bin/env python 2 # _author: bruce 3 # @DATe : 2017-09-08 4 5 proud_list = [ 6 ('iphone6s',6800), 7 ('ipad',3000), 8 ('mac book',9800) 9 ] 10 ... ...
1 #!/usr/bin/env python 2 # _author: bruce 3 # @DATe : 2017-09-08 4 5 proud_list = [ 6 ('iphone6s',6800), 7 ('ipad',3000), 8 ('mac book',9800) 9 ] 10 #接收用戶輸入已有的錢金額 11 money = input("please input your money:") 12 #購物車列表 13 sp = [] 14 15 #判斷用戶輸入的是否合法 16 if money.isdigit(): 17 money = int(money) 18 while True: 19 #列印商品列表 20 for i,v in enumerate(proud_list,1): 21 print(i,":",v) 22 #用戶輸入,選擇商品列表 23 choice = input("請輸入序號選擇商品(q,退出):") 24 #判斷用戶輸入是否合法 25 if choice.isdigit(): 26 choice = int(choice) 27 if choice >0 and choice <= len(proud_list): 28 #將用戶輸入的商品取出來 29 p_item = proud_list[choice-1] 30 if p_item[1] < money: 31 money -= p_item[1] 32 sp.append(p_item) 33 else: 34 print("餘額不足!餘額為%s元"% money) 35 print(p_item) 36 else: 37 print("商品序號不存在!") 38 elif choice == 'q': 39 print("--------您已經購買了以下商品--------") 40 #迴圈遍歷列印已購買的商品 41 for i in sp: 42 print(i) 43 print("您還剩餘%s元",money) 44 break 45 else: 46 print("輸入錯誤")