1、購物車1.0
需求:
(1)打印產品列表裏的產品
(2)寫一個循環不斷問用戶想買什麼,用戶選擇一個商品編號,把對應的商品編號添加到購物車,打印購物車裏的商品列表html
代碼:git
1 # __author__ = "wyb" 2 # date: 2018/4/7 3 # 需求: 4 # (1)打印產品列表裏的產品 5 # (2)寫一個循環不斷問用戶想買什麼,用戶選擇一個商品編號, 6 # 把對應的商品編號添加到購物車,打印購物車裏的商品列表 7 8 # 產品列表: 9 products = [['IPhone8', 6888], ['MacPro', 14688], ['Coffee', 21], ['book', 41], ['Nike Shoes', 999]] 10 11 # 購物車中的商品列表: 12 shopping_car = [] 13 14 while True: 15 # 打印商品列表: 16 print("商品列表".center(30, '-')) 17 for index, p in enumerate(products): 18 print("%s: %s %s" % (index, p[0], p[1])) 19 20 # 輸入及輸入處理及輸出購物車裏的商品列表: 21 # 輸入: 22 choice = input("輸入想購買的物品(輸入q退出): ") 23 # 退出: 24 if choice == 'q' or choice == 'Q': 25 # 輸出購物車裏的商品列表: 26 if shopping_car is []: 27 print("您沒有購買任何商品") 28 else: 29 print("您購買了如下的商品".center(30, '-')) 30 for index, p in enumerate(shopping_car): 31 print("%s: %s %s" % (index, p[0], p[1])) 32 break 33 # 輸入處理: 34 # 是數字: 35 elif choice.isdigit(): 36 choice = int(choice) 37 if choice >= len(products) or choice < 0: 38 print("請輸入正確的序號!") 39 continue 40 # 不是數字: 41 else: 42 print("請輸入數字序號!") 43 continue 44 45 # 向購物車中添加商品: 46 shopping_car.append(products[choice]) 47 print("Add product %s into the shopping car" % (products[choice]))
2、購物車2.0數據結構
基礎需求:app
一、啓動程序後,輸入用戶名密碼後,讓用戶輸入工資,而後打印商品列表函數
二、容許用戶根據商品編號購買商品spa
三、用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒code
四、可隨時退出,退出時,打印已購買商品和餘額htm
五、在用戶使用過程當中, 關鍵輸出,如餘額,商品已加入購物車等消息,需高亮顯示blog
注: 基礎需求中的高亮顯示可見個人另一篇博客: http://www.cnblogs.com/wyb666/p/8850276.htmlip
擴展需求:
一、用戶下一次登陸後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的餘額什麼的仍是那些,再次登陸可繼續購買
二、容許查詢以前的消費記錄
數據結構:
1 goods = [ 2 {"name": "電腦", "price": 1999}, 3 {"name": "鼠標", "price": 10}, 4 {"name": "遊艇", "price": 20}, 5 {"name": "美女", "price": 998}, 6 ...... 7 ]
代碼:
基礎需求實現:
1 # 用戶信息 2 user_info = { 3 'wyb': '666', 4 } 5 6 # 商品列表 7 goods = [ 8 {"name": "電腦", "price": 1999}, 9 {"name": "鼠標", "price": 10}, 10 {"name": "遊艇", "price": 20}, 11 {"name": "美女", "price": 998}, 12 ] 13 14 # 購物車列表 -> [{"name": "電腦", "price": 1999, "number": 1}...] 15 shopping_cart = [] 16 17 18 # 高亮輸出字符串 19 def output_highlight(s): 20 print('\033[1;31;40m%s\033[0m' % s) 21 22 23 # 打印商品列表 24 def output_info(): 25 print("info".center(36, '*')) 26 print("\t\t%s\t%s" % ("name", "price")) 27 for index, item in enumerate(goods): 28 print("\t%d. %s\t\t%d " % (index+1, item['name'], item['price'])) 29 print("end".center(36, '*')) 30 31 32 # 退出時打印易購買商品和餘額 33 def output_shopping_salary(): 34 output_highlight('\t\t你還剩下: %d元\033[0m' % salary) 35 print("shopping_cart".center(36, '-')) 36 print("\t\t%s\t%s\t%s" % ("name", "price", "number")) 37 for index, item in enumerate(shopping_cart): 38 print("%5d.%4s%8d%8d " % (index + 1, item['name'], item['price'], item['number'])) 39 print("end".center(36, '-')) 40 41 42 # 選擇函數,專門負責輸入處理 43 def input_choice(string): 44 choices = input(string).strip() 45 if choices == 'q': 46 # 退出時,打印已購買商品和餘額 47 output_shopping_salary() 48 exit() 49 50 return choices 51 52 53 # 登陸驗證程序: 54 def login(): 55 count = 0 56 while True: 57 count += 1 58 username = input_choice("username: ") 59 password = input_choice("password: ") 60 # 當輸入3次而且第3次也錯誤的狀況下用戶不能再登陸 61 if count >= 3 and user_info.get(username, None) != password: 62 print("你已經錯了太屢次了!不能再試了!") 63 exit() 64 if user_info.get(username, None) == password: 65 print("登陸成功!") 66 return 0 67 else: 68 print("用戶名或密碼錯誤!") 69 continue 70 71 72 # 主函數: 73 74 # 登陸: 75 print("購物車系統: 輸入q將退出系統") 76 login() 77 78 # 輸入工資 79 salary = input_choice("輸入你的工資: ") 80 salary = int(salary) 81 82 # 打印商品列表 83 output_info() 84 85 # 購買商品 86 while True: 87 # 輸入商品序號 88 good_choice = int(input_choice("請輸入想購買的商品的序號: ")) 89 # 獲取商品字典和商品價格 90 good_dict = goods[good_choice - 1] 91 price = good_dict['price'] 92 93 # 沒錢了: 94 if salary == 0: 95 # 高亮輸出花完了錢 96 output_highlight("你的工資已經花完了!") 97 break 98 99 # 能夠買的狀況: 100 elif price <= salary: 101 # 獲取商品名字 102 name = good_dict['name'] 103 # 遍歷購物車,若是購物車中已有該商品把商品數直接加一,不然就加入商品 104 for index, shopping_good in enumerate(shopping_cart): 105 # 找到商品,商品數加一 106 if shopping_good['name'] == name: 107 shopping_good['number'] += 1 108 break 109 else: 110 # 加入商品 111 good_dict['number'] = 1 112 shopping_cart.append(good_dict) 113 # 在薪水中減去花的錢 114 salary -= price 115 # 高亮顯示餘額及加入購物車 116 output_highlight("%s已加入購物車" % name) 117 output_highlight("你還有%d元錢" % salary) 118 continue 119 # 不能夠買的狀況: 120 else: 121 # 高亮輸出餘額不足 122 output_highlight("餘額不足,請換一件商品或退出系統!") 123 continue 124 125 # 退出時,打印已購買商品和餘額 126 output_shopping_salary()
升級需求實現:
(1)加上第一個升級需求,每一次的餘額會保存到下一次
1 # 用戶信息 2 user_info = { 3 'wyb': '666', 4 } 5 6 # 打開文件讀取第一行數據, 即爲用戶工資 7 f = open("money.txt", "r", encoding="utf-8") 8 salary = int(f.readline()) 9 f.close() 10 11 # 商品列表 12 goods = [ 13 {"name": "電腦", "price": 1999}, 14 {"name": "鼠標", "price": 10}, 15 {"name": "遊艇", "price": 20}, 16 {"name": "美女", "price": 998}, 17 ] 18 19 # 購物車列表 -> [{"name": "電腦", "price": 1999, "number": 1}...] 20 # 升級需求 -> 存入文件中 21 shopping_cart = [] 22 23 24 # 高亮輸出字符串 25 def output_highlight(s): 26 print('\033[1;31;40m%s\033[0m' % s) 27 28 29 # 打印商品列表 30 def output_info(): 31 print("info".center(36, '*')) 32 print("\t\t%s\t%s" % ("name", "price")) 33 for index, item in enumerate(goods): 34 print("\t%d. %s\t\t%d " % (index+1, item['name'], item['price'])) 35 print("end".center(36, '*')) 36 37 38 # 退出時打印已購買商品和餘額 39 def output_shopping_salary(): 40 # 打印餘額 41 output_highlight('\t\t你還剩下: %d元\033[0m' % salary) 42 # 將餘額存入文件中 43 f1 = open("money.txt", "w", encoding="utf-8") 44 f1.write(str(salary)) 45 f1.close() 46 # 打印已購買商品 47 print("shopping_cart".center(36, '-')) 48 print("\t\t%s\t%s\t%s" % ("name", "price", "number")) 49 for index, item in enumerate(shopping_cart): 50 print("%5d.%4s%8d%8d " % (index + 1, item['name'], item['price'], item['number'])) 51 print("end".center(36, '-')) 52 53 54 # 選擇函數,專門負責輸入處理 55 def input_choice(string): 56 choices = input(string).strip() 57 if choices == 'q': 58 # 退出時,打印已購買商品和餘額 59 output_shopping_salary() 60 exit() 61 62 return choices 63 64 65 # 登陸驗證程序: 66 def login(): 67 count = 0 68 while True: 69 count += 1 70 username = input_choice("username: ") 71 password = input_choice("password: ") 72 # 當輸入3次而且第3次也錯誤的狀況下用戶不能再登陸 73 if count >= 3 and user_info.get(username, None) != password: 74 print("你已經錯了太屢次了!不能再試了!") 75 exit() 76 if user_info.get(username, None) == password: 77 print("登陸成功!") 78 return 0 79 else: 80 print("用戶名或密碼錯誤!") 81 continue 82 83 84 # 主函數: 85 86 # 登陸: 87 print("購物車系統: 在任意地方輸入q將退出系統") 88 login() 89 90 # 輸入工資 91 if salary == 0: 92 salary = input_choice("輸入你的工資: ") 93 salary = int(salary) 94 95 # 打印商品列表及工資 96 output_info() 97 print("你有%d元錢" % salary) 98 99 # 購買商品 100 while True: 101 # 輸入商品序號 102 good_choice = int(input_choice("請輸入想購買的商品的序號: ")) 103 # 獲取商品字典和商品價格 104 good_dict = goods[good_choice - 1] 105 price = good_dict['price'] 106 107 # 沒錢了: 108 if salary == 0: 109 # 高亮輸出花完了錢 110 output_highlight("你的工資已經花完了!") 111 break 112 113 # 能夠買的狀況: 114 elif price <= salary: 115 # 獲取商品名字 116 name = good_dict['name'] 117 # 遍歷購物車,若是購物車中已有該商品把商品數直接加一,不然就加入商品 118 for index, shopping_good in enumerate(shopping_cart): 119 # 找到商品,商品數加一 120 if shopping_good['name'] == name: 121 shopping_good['number'] += 1 122 break 123 else: 124 # 加入商品 125 good_dict['number'] = 1 126 shopping_cart.append(good_dict) 127 # 在薪水中減去花的錢 128 salary -= price 129 # 高亮顯示餘額及加入購物車 130 output_highlight("%s已加入購物車" % name) 131 output_highlight("你還有%d元錢" % salary) 132 continue 133 # 不能夠買的狀況: 134 else: 135 # 高亮輸出餘額不足 136 output_highlight("餘額不足,請換一件商品或退出系統!") 137 continue 138 139 # 退出時,打印已購買商品和餘額 140 output_shopping_salary()
(2)兩個升級需求所有實現
1 # __author__ = "wyb" 2 # date: 2018/4/18 3 4 5 # 用戶信息 6 user_info = { 7 'wyb': '666', 8 } 9 10 # 打開文件讀取第一行數據, 即爲用戶工資 11 f = open("money.txt", "r", encoding="utf-8") 12 salary = int(f.readline()) 13 f.close() 14 15 # 商品列表 16 goods = [ 17 {"name": "電腦", "price": 1999}, 18 {"name": "鼠標", "price": 10}, 19 {"name": "遊艇", "price": 20}, 20 {"name": "美女", "price": 998}, 21 ] 22 23 # 購物車列表 -> [{"name": "電腦", "price": 1999, "number": 1}...] 24 # 升級需求 -> 存入文件中 25 shopping_cart = [] 26 27 28 # 高亮輸出字符串 29 def output_highlight(s): 30 print('\033[1;31;40m%s\033[0m' % s) 31 32 33 # 打印商品列表 34 def output_info(): 35 print("商品列表".center(36, '*')) 36 print("\t\t%s\t%s" % ("name", "price")) 37 for index, item in enumerate(goods): 38 print("\t%d. %s\t\t%d " % (index+1, item['name'], item['price'])) 39 print("end".center(36, '*')) 40 41 42 # 退出以前將購物車的數據以添加的方式寫入文件中 43 def before_exit(): 44 file = open("shopping_cart.txt", "a+", encoding="utf-8") 45 for item in shopping_cart: 46 file.write(str(item)+"\n") 47 file.close() 48 49 50 # 打印購物記錄 51 def output_shopping_records(): 52 file = open("shopping_cart.txt", "r", encoding="utf-8") 53 s = file.readlines() 54 print("購物記錄以下: ") 55 if not s: 56 print("無購物記錄") 57 else: 58 print("商品 單價 數量") 59 for item in s: 60 item = eval(item.strip()) 61 # print(type(item)) 62 print(item['name'], str(item['price'])+"元", item['number']) 63 output_highlight("你還有%d元錢" % salary) 64 file.close() 65 66 67 # 退出時打印已購買商品和餘額 68 def output_shopping_salary(): 69 # 打印餘額 70 output_highlight('\t\t你還剩下: %d元\033[0m' % salary) 71 # 將餘額存入文件中 72 f1 = open("money.txt", "w", encoding="utf-8") 73 f1.write(str(salary)) 74 f1.close() 75 # 打印已購買商品 76 print("shopping_cart".center(36, '-')) 77 print("\t\t%s\t%s\t%s" % ("name", "price", "number")) 78 for index, item in enumerate(shopping_cart): 79 print("%5d.%4s%8d%8d " % (index + 1, item['name'], item['price'], item['number'])) 80 print("end".center(36, '-')) 81 82 83 # 選擇函數,專門負責輸入處理 84 def input_choice(string): 85 choices = input(string).strip() 86 if choices == 'q': 87 # 退出以前 88 before_exit() 89 # 退出時,打印已購買商品和餘額 90 output_shopping_salary() 91 exit() 92 93 return choices 94 95 96 # 登陸驗證程序: 97 def login(): 98 count = 0 99 while True: 100 count += 1 101 username = input_choice("username: ") 102 password = input_choice("password: ") 103 # 當輸入3次而且第3次也錯誤的狀況下用戶不能再登陸 104 if count >= 3 and user_info.get(username, None) != password: 105 print("你已經錯了太屢次了!不能再試了!") 106 exit() 107 if user_info.get(username, None) == password: 108 print("登陸成功!") 109 return 0 110 else: 111 print("用戶名或密碼錯誤!") 112 continue 113 114 115 # 主函數: 116 117 # 登陸: 118 print("購物車系統: 在任意地方輸入q將退出系統") 119 c = input("輸入c查詢以前的購物記錄, 輸入q退出系統,輸入其餘繼續訪問: ") 120 if c == 'c': 121 output_shopping_records() 122 exit() 123 elif c == 'q': 124 exit() 125 else: 126 login() 127 128 # 輸入工資 129 if salary == 0: 130 salary = input_choice("輸入你的工資: ") 131 salary = int(salary) 132 133 # 打印商品列表及工資 134 output_info() 135 output_highlight("你還有%d元錢" % salary) 136 137 # 購買商品 138 while True: 139 # 輸入商品序號 140 good_choice = int(input_choice("請輸入想購買的商品的序號: ")) 141 # 獲取商品字典和商品價格 142 good_dict = goods[good_choice - 1] 143 price = good_dict['price'] 144 145 # 沒錢了: 146 if salary == 0: 147 # 高亮輸出花完了錢 148 output_highlight("你的工資已經花完了!") 149 break 150 151 # 能夠買的狀況: 152 elif price <= salary: 153 # 獲取商品名字 154 name = good_dict['name'] 155 # 遍歷購物車,若是購物車中已有該商品把商品數直接加一,不然就加入商品 156 for index, shopping_good in enumerate(shopping_cart): 157 # 找到商品,商品數加一 158 if shopping_good['name'] == name: 159 shopping_good['number'] += 1 160 break 161 else: 162 # 加入商品 163 good_dict['number'] = 1 164 shopping_cart.append(good_dict) 165 # 在薪水中減去花的錢 166 salary -= price 167 # 高亮顯示餘額及加入購物車 168 output_highlight("%s已加入購物車" % name) 169 output_highlight("你還有%d元錢" % salary) 170 continue 171 # 不能夠買的狀況: 172 else: 173 # 高亮輸出餘額不足 174 output_highlight("餘額不足,請換一件商品或退出系統!") 175 continue 176 177 # 退出以前 178 before_exit() 179 180 # 退出時,打印已購買商品和餘額 181 output_shopping_salary()