功能要求:
额度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 "没有这个账户。"