Python作業2,購物車程序
阿新 • • 發佈:2017-09-26
eas 作業 break [] true else 檢測 auth bsp
程序:購物車程序
需求:
- 啟動程序後,讓用戶輸入工資,然後打印商品列表
- 允許用戶根據商品編號購買商品
- 用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
- 可隨時退出,退出時,打印已購買商品和余額
購物車作業代碼
1 #Author:abu zhang 2 3 import sys 4 shop_list = [ 5 ("IphoneX",9000), 6 ("mac book",19000), 7 ("IphoneX",9000), 8 ("Netbook computer",5000), 9 ("linux python book",90),10 ("coffee",54) 11 ] 12 shopping_cart = [] 13 try: 14 salary = input("Please enter your salary:") 15 if salary.isdigit(): 16 salary = int(salary) 17 while True: 18 for k, v in enumerate(shop_list): 19 print(k, v) 20 user_input = input("Please choose the item you want to buy ,input ‘quit‘ exit:") 21 22 if user_input.isdigit(): 23 user_input = int(user_input) 24 if user_input < len(shop_list) and user_input >= 0: 25 p_item = shop_list[user_input] 26 ifsalary > p_item[1]: 27 shopping_cart.append(p_item) 28 salary = salary - p_item[1] 29 print("you have purchased \033[31;1m%s\033[0m; shopping,amount remaining \033[31;1m%s\033[0m"%(p_item[0],salary)) 30 else: 31 print("您的余額不足矣購買該商品。") 32 else: 33 print("\033[31;1m找不到您想要購買的商品,請重新輸入\033[0m") 34 elif user_input == "quit": 35 print("歡迎下次光臨!") 36 print("您已購買了如下商品:") 37 print("\t\t------shop list-----") 38 for i in shopping_cart: 39 print("\t\t",i) 40 break 41 finally: 42 print("Input Error")
Python作業2,購物車程序