前言:這是初學時寫的小項目,以爲有意思就寫來玩玩,也當是鞏固剛學習的知識。如今看來很不成熟,但仍是記錄一下作個記念好了~python
一、名稱:網上網上銀行及購物商城 git
二、項目結構:github
當時剛接觸python啦,哪裏注意什麼項目結構,就一腦子全塞到一個文件裏面了數據庫
三、效果:app
四、主要功能ide
1.網上銀行功能:函數
2.購物商城功能:學習
五、設計過程字體
5.1首先設置admin.txt中的用戶名和密碼spa
5.2很騷氣的設置打印出字體的顏色
# 設置打印字體的顏色 class change_color: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' def disable(self): self.HEADER = '' self.OKBLUE = '' self.OKGREEN = '' self.WARNING = '' self.FAIL = '' self.ENDC = ''
5.3驗證賬號的函數
判斷賬號是否存在、鎖定、密碼是否正確,三次密碼不正確就鎖定該用戶(寫入lock 文件)
# 判斷賬號是否存在 def admin_is_exist(user_admin): with open(admin_file, 'rb') as li: for i in li.readlines(): i = i.strip().split() if user_admin in i: return True else: print '\n\t\t\t'+change_color.WARNING+'This admin is not exist! Please try again!'+change_color.ENDC return False # 判斷賬號是否被鎖定 def admin_is_locked(user_admin): with open(lock_file, 'rb') as lock: for i in lock.xreadlines(): i = i.strip().split() if user_admin in i: print '\n\t\t\t'+change_color.WARNING+'This admin in locked! Please try another admin!'+change_color.ENDC return False else: return True # 判斷密碼是否匹配 def password_is_match(user_admin, pass_word): with open(admin_file, 'rb') as admin: for i in admin.readlines(): i = i.strip().split() if user_admin in i: if pass_word == i[1]: return True else: return False # 鎖定用戶 def lock_user(user_admin): lock = open(lock_file, 'ab') lock.write('\n' + user_admin) lock.close() print '\n\t\t\t'+change_color.WARNING+'Password do not match admin for 3 times! Admin is locked!'+change_color.ENDC
5.4網上銀行相關函數
給每一個用戶創建一個以該用戶名爲標題的帳單、寫入月帳單、寫入並讀取餘額、取錢、打印用戶流水
# 計算提現所需的餘額(包括手續費) def caculate_cash_with_fee(cash): total = cash + cash * 0.05 return total # 計算取現後的餘額 def balance_caculate(cash, balance): total = balance - cash - cash * 0.05 return total # 初始化餘額,若是有餘額就不變,沒有就初始化一個 (出錯,文件爲空) def create_dict_in_balance(): filename = balance_file if os.path.exists(filename): if os.path.getsize(filename): return 0 else: fw = open(filename, 'wb') user_balance = {'weiwei': 1520000, 'huahua': 52000, 'xiaoji': 100} pickle.dump(user_balance, fw) fw.close() return 'new one' # 存儲餘額消息 # 每次初始化都替代,要改 def save_balance(user_admin, balance): fr = open(balance_file, 'rb') user_balance = pickle.load(fr) user_balance[user_admin] = balance fr.close() fw = open(balance_file, 'wb') pickle.dump(user_balance, fw) fw.close() # 在每一個用戶的流水文件中存入一個新的名字相同的列表 def create_list_in_accout(user_admin): filename = u'%s\\%s.pk' % BASE_DIR, user_admin if os.path.exists(filename): # 若是文件存在 with open(filename, 'rb') as f: li = pickle.load(f) if 'accout_list' == li: # 列表存在,不改變 return 0 else: # 文件或列表不存在,建立 fw = open(filename, 'wb') accout_list = [] pickle.dump(accout_list, fw) fw.close() return 'new list' # 存儲用戶流水 (序列化存儲多個數據時最好使用列表等方式) # 要改爲‘withdraw’和‘shop’兩種形式,增長一個參數 def save_current_accout(user_admin, way, cash, balance): current_accout = 'time: %s %s: %d balance: %d' % (time.strftime('%y-%m-%d %H:%M:%S'), way, cash, balance) fr = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'rb') accout_list = pickle.load(fr) accout_list.append(current_accout) # 將新的流水寫入列表 fr.close() fw = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'wb') pickle.dump(accout_list, fw) fw.close() return current_accout # 打印用戶流水 def print_user_accout(user_admin): f = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'rb') accout_list = pickle.load(f) f.close() for i in accout_list: print i # 讀出某用戶文檔中的餘額 def read_balance(user_admin): f = open(balance_file, 'rb') user_balance = pickle.load(f) f.close() return user_balance[user_admin] # 讀出餘額的字典 def read_balance_dict(): f = open(balance_file, 'rb') user_balance = pickle.load(f) f.close() return user_balance
5.5主函數
購物這個功能是以後加的,因此直接在注函數實現,這樣會讓主函數十分冗餘,不推薦。
主函數就是一些基本的邏輯判斷啊,人機交互之類的
# (全局變量)用戶餘額信息 shopping = {'orange': 30, 'milk': 50, 'bike': 200, 'lipstick': 350, 'bag': 3000, 'car': 100000, 'house': 1200000} # 主程序 if __name__ == '__main__': print '\n\t\t\t\t'+change_color.HEADER+'Welcome to Internet Bank!'+change_color.ENDC # 歡迎界面 login = 1 # 爲0表示已經登陸進去了,爲1表示還沒有登陸成功 while login: # 登陸主程序 user_admin = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your admin name:'+change_color.ENDC) # 輸入賬號 if admin_is_locked(user_admin): #判斷賬號是否被鎖定 if admin_is_exist(user_admin): #判斷賬號是否存在 times = 3 while times:# 能夠輸入密碼三次 pass_word = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your password:'+change_color.ENDC) # 輸入密碼 if password_is_match(user_admin, pass_word): # 密碼正確,打印帳戶資料 print '\n\t\t\t\t'+change_color.HEADER+'Welcome!'+change_color.ENDC login = 0 break else: times -= 1 print '\n\t\t\t'+change_color.WARNING+"Password do not match! You still have "+str(times)+" times to try!" + change_color.ENDC if times == 0: # 輸入密碼三次不正確,鎖定用戶 lock_user(user_admin) print '\n\t\t\t'+change_color.WARNING+"%s is locked " %user_admin + change_color.ENDC sys.exit() # 網上銀行界面 if login == 0: create_list_in_accout(user_admin) create_dict_in_balance() user_interface = 1 while user_interface: print '\n\t\t\t'+change_color.OKBLUE+''' ################################################# user name: %s user balance: %s Operation: 1.withdraw cash 2.shopping 3.month account 4.exit ################################################# ''' % (user_admin, read_balance(user_admin)) + change_color.ENDC # 用戶選擇要進行的操做 operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4):'+change_color.ENDC) if operation == 1: # 提現操做 cash_interface = 1 while cash_interface: cash = input('\n\t\t\t'+change_color.OKBLUE+'Please input cash you want:'+change_color.ENDC) user_balance = read_balance_dict() if user_balance[user_admin] >= caculate_cash_with_fee(cash): # 提現的錢不超過餘額 if cash <= 15000: # 小於額度 balance = balance_caculate(cash, user_balance[user_admin]) # 計算新的餘額 print '\n\t\t\t'+change_color.OKGREEN+'You can take your cash! Your card still have %s:' % balance + change_color.ENDC # 將餘額寫入文檔 save_balance(user_admin, balance) # 將流水寫入文檔(作成函數) save_current_accout(user_admin, 'withdraw cash', cash, balance) else: print '\n\t\t\t'+change_color.WARNING+'You can not take more than 15000!'+change_color.ENDC else: print '\n\t\t\t'+change_color.WARNING+'You balance is not enough!'+change_color.ENDC # 選擇是否繼續 choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Do you want to continue?(y/n):'+change_color.ENDC) if choice == 'y': # 繼續提現 continue else: # 返回主界面 cash_interface = 0 if operation == 2: # 購物車系統 buy_count = {} # 存放已經買的東西的種類 buy_counts = [] # 全部東西都直接加到購物車中 buy_balance = read_balance(user_admin) can_buy = 1 while can_buy: print '\n\t\t\t\t'+change_color.OKBLUE+'Welcome for shopping!'+change_color.ENDC print print '\n\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDC for i in enumerate(sorted(shopping.items(), key = lambda item:item[1]), 1): # 將字典中的元素轉換成元組排序,編號並輸出 print '\t\t\t\t'+change_color.OKBLUE+'%d %s %d'% (i[0], i[1][0], i[1][1]) + change_color.ENDC print '\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDC # 讀取用戶餘額 print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC # 用戶選擇要進行的操做 buy = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to buy(1/2/3/4/...):'+change_color.ENDC) shopping_list = sorted(shopping.items(), key=lambda item: item[1]) if buy_balance > shopping_list[buy - 1][1]: # 工資卡里的錢能夠購買這樣東西 # 把東西放到購物車裏 buy_counts.append(shopping_list[buy - 1][0]) print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC # 計算新的餘額 buy_balance -= shopping_list[buy - 1][1] else: # 錢不夠, 使用信用額度 credit = raw_input( '\n\t\t\t'+change_color.WARNING+'Balance is not enough! Using credit(y/n)?'+change_color.ENDC) if credit =='y': # 把東西放到購物車裏 buy_counts.append(shopping_list[buy - 1][0]) print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC # 計算新的餘額 buy_balance -= shopping_list[buy - 1][1] choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) # 是否繼續 if choice == 'y': continue if choice == 'n': finish = 1 while finish: print '\n\t\t\t'+change_color.OKBLUE+''' ################################################# Operation: 1.watch shopping car 2.add 3.reduce 4.accout 5.exit to user interface 6.exit system ################################################# ''' + change_color.ENDC # 用戶選擇要進行的操做 buy_operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4/5):'+change_color.ENDC) if buy_operation == 1: # 查看購物車 # 購物車列表轉換爲集合去重 buy_count = set(buy_counts) buy_list = list(buy_count) print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC # 打印選購的商品價格和數量 for i in enumerate(buy_list, 1): print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) + change_color.ENDC print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC # 打印總價 print '\n\t\t\t'+change_color.WARNING+ '10s later will come back to shopping operate interface' +change_color.ENDC time.sleep(10) if buy_operation == 2: # 增長購物車裏的東西 finish = 0 if buy_operation == 3: # 減去購物車裏的東西 undo_finish = 1 while undo_finish: # 讀取用戶餘額 print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC # 打印購物車 buy_count = set(buy_counts) buy_list = list(buy_count) print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC # 打印選購的商品價格和數量 for i in enumerate(buy_list, 1): print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) + change_color.ENDC print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC # 打印總價 # 用戶選擇要進行的操做 undo = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to reduce(1/2/...):'+change_color.ENDC) if buy_list[undo - 1] in buy_counts: # 若是商品在購物車中,刪除 buy_counts.remove(buy_list[undo - 1]) print '\n\t\t\t'+change_color.OKGREEN+'%s is delete from you shopping car' % shopping_list[undo - 1][0] +change_color.ENDC # 計算新的餘額 buy_balance += shopping[buy_list[undo - 1]] else: # 購物車裏沒有該物品 print '\n\t\t\t'+change_color.WARNING+'%s is not in you shopping car' % buy_list[undo - 1]+change_color.ENDC undo_choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) # 是否繼續 if undo_choice == 'y': continue if undo_choice == 'n': undo_finish = 0 if buy_operation == 4: # 結算 buy_count = set(buy_counts) buy_list = list(buy_count) print '\n\t\t\t\t'+change_color.OKBLUE+'name price amount' + change_color.ENDC # 打印選購的商品價格和數量 for i in buy_list: print '\t\t\t\t'+change_color.OKBLUE+'%s %d %d'% (i, shopping[i], buy_counts.count(i)) + change_color.ENDC total = read_balance(user_admin) - buy_balance print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % total +change_color.ENDC # 打印總價 pay = raw_input('\n\t\t\t'+change_color.OKGREEN+'Do you want to pay(y/n)?'+change_color.ENDC) if pay == 'y': # 確認付款,將流水和餘額寫入文件 # 餘額 save_balance(user_admin, buy_balance) print '\n\t\t\t'+change_color.OKGREEN+'Successful payment!'+change_color.ENDC print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC # 流水寫入文件 save_current_accout(user_admin, 'shopping', total, buy_balance) finish = 0 # 返回主界面 can_buy = 0 time.sleep(3) if pay == 'n': # 若是不付款,返回商品操做界面 can_buy = 0 if buy_operation == 5: # 退回主界面 print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC finish = 0 can_buy = 0 if buy_operation == 6: # 退出 print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC sys.exit() if not (buy_operation == 1 or buy_operation == 2 or buy_operation == 3 or buy_operation == 4 or buy_operation == 5 or buy_operation == 6): print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC time.sleep(3) if operation == 3: # 查看流水帳操做 #'dict' object has no attribute 'readline print_user_accout(user_admin) print '\n\t\t\t'+change_color.WARNING+'10s later will come back to user interface'+change_color.ENDC time.sleep(10) if operation == 4: # 退出系統 print '\n\t\t\t\t'+change_color.HEADER+'Thanks for using Internet Bank!'+change_color.ENDC sys.exit() if not (operation == 1 or operation == 2 or operation == 3 or operation == 4): print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC time.sleep(3)
整個系統的邏輯其實很簡單,說一下我在這個過程當中遇到的問題吧。
因爲我剛剛接觸這個語言,代碼寫的很是不整潔,冗餘部分不少(個人主函數太長了,長到我本身都有點懵),其實能夠把不少部分單獨拎出來做爲一個函數,這樣整個項目的可讀性比較好。還有不少不足之處,一時也說不上來,等我回去想一想~
我會把整個項目貼在個人github上,歡迎你們來提供意見~
完整項目代碼 :https://github.com/huahua462/bank-sopping