學習了list,試寫一段購物車程序
阿新 • • 發佈:2017-07-30
tar 打印 iphone print 商品 usr author 列表 logs
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Author:Mgin 4 shopping_car = [] 5 salary = int(input("your salary:")) 6 goods_list = [["Watch",20000],["ThinkPad",8500],["IPhone",5800 ],["Suit",3700],["Boost shoes",1700],["Contact Lens",150],["Alex python",80],["Starbucks",30]] 7 print("goods list:") 8 fori in goods_list: 9 print(goods_list.index(i),i) 10 while salary > 0: 11 add = int(input("add goods to your shopping car:")) 12 if add > int(len(goods_list)-1) or add < 0: 13 print("goods have not been found ") 14 stop = input("Do you want to continue:") 15 ifstop != ‘n‘: 16 continue 17 else: 18 for i in shopping_car: 19 print(i) 20 print("balance:", salary) 21 break 22 elif salary - goods_list[add][1] > 0: 23 salary = salary - goods_list[add][1] 24 shopping_car.append(goods_list[add])25 print("added",goods_list[add][0],"to your shopping car.","\n","balance:",salary) 26 stop = input("Do you want to continue:") 27 if stop != ‘n‘: 28 continue 29 else: 30 for i in shopping_car: 31 print(i) 32 print("balance:",salary) 33 break 34 else: 35 print("you have not enough money") 36 stop = input("Do you want to continue:") 37 if stop != ‘n‘: 38 continue 39 else: 40 for i in shopping_car: 41 print(i) 42 print("balance:", salary) 43 break
程序:購物車
需求:
1.啟動程序後,讓用戶輸入工資,然後打印商品列表
2.允許用戶根據商品編號購買商品
3.用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
4.可隨時退出,退出時,打印已購買商品和余額
學習了list,試寫一段購物車程序