功能要求:
額度15000
可以提現,手續費5%
記錄消費流水
支持每月賬單查詢
提供還款接口
調用的相關模塊:
import time #time模塊,提供各種操作時間的函數 time.strftime("%Y-%m-%d %X", time.localtime()) '2015-12-15 23:35:26'
import getpass #getpass 模塊 -> 命令行下輸入密碼的方法 pwd = getpass.getpass('password: ') print pwd
import pickle #pickle模塊,序列化Python對象 #保存數據到 Pickle 文件 >>> dic = {} >>> import pickle >>> with open('dic.pickle', 'wb') as f: ... pickle.dump(dic, f) ... #從Pickle文件讀取數據 >>> import pickle >>> with open('dic.pickle', 'rb') as f: ... dic = pickle.load(f) ...
1、用戶數據通過字典序列化存入account.pickle
cat account.py #!/usr/bin/env python #coding=utf-8 import pickle account = {'1001':{'name':'fgf','pw':123,'money':15000,'balance':15000}, '1002':{'name':'user','pw':123,'money':15000,'balance':15000}} f = file('account.pickle','wb') pickle.dump(account,f) f.close() with open('account.pickle', 'rb') as f: dic = pickle.load(f) f.close() print dic
2、Python程序
#!/usr/bin/env python #coding=utf-8 import sys,pickle,getpass,time #日誌函數 def log(user,describe,fee,balance,**userinfo): time1 = time.strftime("%Y-%m-%d %X", time.localtime()) f = file('account.log','a') f.write("%s %s %s ¥%s ¥%s\n" %(time1,user,describe,fee,balance)) f.close() f1 = file('account.pickle','wb') pickle.dump(userinfo,f1) f1.close() #提現函數 def withdraw(user,**userinfo): fee = int(raw_input("請輸入提現金額:")) if fee % 100 ==0 and int(fee*1.05) <= userinfo[user]['balance']: charge = fee * 0.05 balance = userinfo[user]['balance'] - fee - charge userinfo[user]['balance'] = balance log(user,"取現(手續費%d)"%charge,fee,balance,**userinfo) print "你成功取現%d,收取手續費%s,剩餘額度%s" %(fee,charge,balance) else: print "輸入金額有誤。" #還款函數 def repay(user,**usrinfo): fee = int(raw_input("請輸入還款金額:")) userinfo[user]['balance'] += fee balance = userinfo[user]['balance'] log(user,"信用卡還款",-fee,balance,**userinfo) print "你成功還款%d,當前可用額度%s" %(fee,balance) #賬單查詢 def bills(user): f = file('account.log','r') time = raw_input("請輸入查詢的時間(格式:yyyy-mm):") match_yes = 0 #匹配標示 for line in f.readlines(): if user in line and time in line: print line, match_yes = 1 if match_yes == 0 : print"沒有查詢月份賬單" with open('account.pickle','rb') as f: userinfo = pickle.load(f) f.close() while True: user = raw_input("\33[1;32;40m請輸入用戶名:\33[0m") i = 1 while userinfo.has_key(user): passwd = int(getpass.getpass("\33[1;32;40m請輸入密碼:\33[0m")) while passwd == userinfo[user]['pw']: i = 1 print "歡迎進入系統,請選擇操作:" choice = int(raw_input("提現 1\t還款 2\t賬單查詢 3\t 額度查詢 4\t 退出 0\n")) if choice == 1: withdraw(user,**userinfo) elif choice == 2: repay(user,**userinfo) elif choice == 3: bills(user) elif choice == 4: print "總額度:",userinfo[user]['money'],"當前額度",userinfo[user]['balance'] elif choice == 0: sys.exit() else: i = i + 1 print"密碼錯誤!" if i == 4 : break else : print "沒有這個賬戶。"