1. 程式人生 > >購物車專案 複雜版本.待簡化

購物車專案 複雜版本.待簡化

  1 # -*- coding: utf-8 -*-
  2 # @Time    : 2018/12/23 13:46
  3 # @Author  : Endless-cloud
  4 # @Site    : 
  5 # @File    : 重寫購物車專案.py
  6 # @Software: PyCharm
  7 '''
  8 0 首先宣告在任何地方輸入q 或者Q 退出系統
  9 1 首先讀取資料夾中的賬號密碼  Y 繼續, N 繼續輸入賬號密碼
 10 2 讓使用者判斷查詢消費記錄 輸入C 或者c ,  Y 2.1  ,N 3
 11     2.1  列印消費,餘額
12 3 列印餘額,讓使用者判斷是否輸入 金額 ,Y 繼續 , N 繼續 13 3.1 餘額充值 ,提示充值成功 ,繼續4 14 4 列印系統商品列表 15 5 讓使用者輸入 想要選擇的商品編號 (擴充套件.商品名稱) 16 6 列印使用者購買的商品名字 ,以及賬戶餘額 17 7 詢問客戶是否繼續購買商品 Y , N 18 7.1 走流程4 ,5 , 6 19 7.2 走流程 0 20 8 如果推出系統打印出客戶已經購買的所有物品 , 以及餘下金額 21 # 所有餘額,商品內容用特殊字標註 22 ''' 23 """
24 準備 : 25 描述 函式名稱 26 1 儲存基礎屬性 __init__ 27 28 描述 名稱 型別 29 1 已經購買的商品列表 shop_listshop list 30 2 儲存使用者的賬號密碼 popmsg dict 31 3 儲存使用者的餘額 ymoney str->int 32 """ 33
34 35 class shop(object): 36 def __init__(self): 37 self.shop_listshop = [] 38 self.popmsg = { # 待升級用讀取txt 39 'wzh': '123' 40 } 41 self.ymoney = '' 42 self.goodgifts = [ 43 {"name": "電腦", "price": 1999}, 44 {"name": "滑鼠", "price": 10}, 45 {"name": "遊艇", "price": 20}, 46 {"name": "美女", "price": 998}, 47 ] 48 self.msg ='' 49 50 # 如果使用者輸入q 那麼 退出系統 51 def close(self, char): 52 if char.upper() == 'Q': 53 self.save() 54 quit() 55 56 # 登入, 有3次判斷機會,如果用完了退出系統 57 def login(self): 58 count = 3 59 while 0 < count: 60 61 username = input('請輸入username') 62 password = input('請輸入password') 63 with open('login.txt', 'r', encoding='utf-8') as f: 64 l = f.readlines() # readlines返回list 65 neirong = eval(l[0]) 66 67 if neirong.get(username) == password: # 利用字典的get()獲取password判斷 68 print('歡迎進入購物系統') 69 break 70 71 elif neirong.get(username) != password: 72 count = count - 1 73 print('你還有%s次機會' % (count)) 74 else: 75 print('機會用完') 76 exit() 77 78 # 開啟money.txt ,讀取第一行 ,利用readline() 獲取內容(str). 在str 型別轉換int型別,然後返回salay 79 def money(self): 80 with open('money.txt', 'r', encoding='utf-8') as f: 81 salay = int(f.readline()) 82 return salay 83 84 # 根據使用者的金額進行判斷充值, 85 def money_chioce(self, salay): 86 print('你當前的餘額為' + str(salay) + '') 87 88 chongzhi = input('是否充值1鍵 ,或直接消費其他除q,c鍵 ,,輸入q退出,c進入已經購買列表') 89 if chongzhi == '1': 90 jine = int(input('請輸入充值金額')) 91 if jine <= 0: 92 endsalay = salay + int(jine) 93 salay = salay + int(jine) 94 print('最少充值1元') 95 96 97 elif jine > 0: 98 print('感謝你的充值') 99 endsalay = salay + int(jine) 100 with open('money.txt', 'w', encoding='utf-8') as f: 101 f.writelines(str(endsalay)) 102 print('你當前的餘額為' + str(endsalay)) 103 self.close(chongzhi) 104 elif chongzhi=='c': 105 self.look_pay_list() 106 print('當前餘額為%s'%(self.money())) 107 elif chongzhi=='q': 108 self.close(chongzhi) 109 110 # 讓使用者輸入c ,打印出來列表中的商品 111 def look_pay_list(self): 112 with open('shaop.txt', 'r', encoding='utf-8') as f: 113 print('%s %s %s' % ('編號', '名字', '數量')) 114 ll = f.readlines() 115 if len(ll)==0: 116 print('當前沒有任何內容') 117 else: 118 s = (eval([i for i in ll][0]) ) 119 # print(type(s)) 120 # print(type(eval([i for i in ll][0])[0])) 121 122 for i, j in enumerate(s): 123 print(str(i) + ' ' + j['name'] + ' ' + str(j['number'])) 124 # print(type(j)) 125 126 127 # 128 def goods(self): 129 goods = self.goodgifts 130 titie1 = '購物系統' 131 print(titie1.center(50, '*')) 132 print('%s %s %s' % ('編號', '名字', '金額')) 133 for i, j in enumerate(goods): 134 print('%s %s %s' % (i + 1, j['name'], j['price'])) 135 def txt_goods(self): 136 with open('shaop.txt', 'r', encoding='utf-8') as f: 137 self.msg=f.readlines() 138 l =eval(self.msg[0]) 139 self.shop_listshop =l 140 # print(type(self.shop_listshop[0])) 141 # print(self.msg) 142 # print(l) 143 # print(type(l[0])) 144 145 def chiose_goods(self): 146 147 while True: 148 chiose2 = input('請輸入購買的商品編號') 149 if chiose2 == 'q': 150 self.close(chiose2) 151 elif chiose2=='c': 152 self.look_pay_list() 153 elif int(chiose2) in [i + 1 for i, j in enumerate(self.goodgifts)]: 154 print('你購買的物品是%s ,花了%s' % (self.goodgifts[int(chiose2) - 1]['name'], self.goodgifts[int(chiose2) - 1]['price'])) 155 goots_dic = [self.goodgifts[int(chiose2) - 1]][0] 156 if self.goodgifts[int(chiose2) - 1]['name'] in [j['name'] for i, j in enumerate(self.shop_listshop)]: 157 self.shop_listshop[int(chiose2) - 1]['number'] = self.shop_listshop[int(chiose2) - 1]['number'] + 1 158 print('yes') 159 else: 160 161 self.shop_listshop.append(goots_dic) 162 self.shop_listshop[int(chiose2) - 1]['number'] = 1 163 print('no') 164 elif chiose2 not in [i + 1 for i, j in enumerate(self.goodgifts)]: 165 print('沒有這個商品編號') 166 print(self.shop_listshop) 167 168 def save(self): 169 with open('shaop.txt', 'w', encoding='utf-8') as f: 170 f.writelines(str(self.shop_listshop)) 171 self.look_pay_list() 172 173 174 shop = shop() 175 176 shop.login() 177 178 l = shop.money() 179 shop.money_chioce(l) 180 shop.goods() 181 shop.chiose_goods()

 



With great power comes great responsibility