当前位置:首页 >> 脚本专栏

python 模拟银行转账功能过程详解

首先画出流程图,流程图与现实代码有出入,因为刚开始画流程图的时候,有些东西没考虑进去,后来写着写着就慢慢能想起来并实现了。

python 模拟银行转账功能过程详解

另有一点经验推荐给新手朋友,如果说碰到一个项目无从下手的话,就先慢慢去写,写着写着你就会往下写了,真的,亲身实践。望大神勿喷~

#!/usr/bin/env python
#encoding:utf-8
import re
import pickle
import time
def getUser():
  '''从数据文件里获取银行卡用户信息'''
  with open('cardinfo.db','r') as f:
    return pickle.load(f)
def panDing():
  '''判定用户银行卡信息跟密码信息的准确性'''
  while True:
    user_dict=getUser()
    print user_dict
    card_num=raw_input('请输入您的19位银行卡号(只包含数字):')#获取用户卡号
    if re.match('\d{19}',card_num) and card_num in user_dict:#判断卡号是否匹配
      card_passwd=(raw_input('请输入您的银行卡密码:'))
      # print '输入的密码是:%s,类型为:%s' % (int(card_passwd),type(int(card_passwd)))
      # print '存的密码是:%s,类型为:%s' % (user_dict[card_num]['password'],type(user_dict[card_num]['password']))
      if int(card_passwd) == user_dict[card_num]['password']:#判定密码对错
        break
      else:
        print '密码错误!'
        continue
    else:
      print '您输入的银行卡信息有误!'
  return card_num

def zhuanZhang(srcaccount):
  '''用户转账操作'''

  user_dict = getUser()
  while True:
    target_account = raw_input('请输入目标账户:')
    if re.match('\d{19}', target_account) :
      if target_account in user_dict: # 判断卡号是否匹配

        while True:
          tr_balance = int(raw_input('请输入转账金额:'))
          if tr_balance <= user_dict[srcaccount]['balance']:#对比转账金额跟账户余额
            break
          else:
            print '转账金额大于余额,请重新输入余额!'
        break
      else:
        print '卡号错误,请重新输入!'
    else:
      print '卡号不对'
  print '转入的账户为:%s ,金额为:%s' % (target_account,tr_balance)
  print '原账户为:%s ,余额为:%s' % (srcaccount,user_dict[srcaccount]['balance'])
  print user_dict
  user_dict[srcaccount]['balance']=user_dict[srcaccount]['balance']-tr_balance
  user_dict[target_account]['balance'] = user_dict[target_account]['balance'] + tr_balance
  print '转入的账户为:%s ,转入的金额为:%s' % (target_account, tr_balance)
  # print '转入账户为:%s ,余额为:%s' % (target_account, user_dict[target_account]['balance'])
  print '原账户为:%s ,余额为:%s' % (srcaccount, user_dict[srcaccount]['balance'])
  print user_dict
  with open('cardinfo.db','w') as f:
    pickle.dump(user_dict,f)
  with open('op.log','a+') as f:
    f.writelines('%s 账户%s转入到账户%s中%s人民币' % (time.strftime('%Y-%m-%d %H:%M:%S'),srcaccount,target_account,tr_balance),f)
    print '%s 账户%s转入到账户%s中%s人民币' % (time.strftime('%Y-%m-%d %H:%M:%S'),srcaccount,target_account,tr_balance)




def quXian(user_card):
  '''用户取现操作'''
  user_dict = getUser()
  while True:
    qx_balance=raw_input('请输入取现金额:')
    if re.match('\d+',qx_balance):
      print user_dict[user_card]['balance']
      if int(qx_balance) <= user_dict[user_card]['balance']:
        user_dict[user_card]['balance'] = user_dict[user_card]['balance'] - int(qx_balance)
        print user_dict
        with open('cardinfo.db', 'w') as f:
          pickle.dump(user_dict, f)
        with open('op.log', 'a') as f:
          f.write('%s 账户 %s 取现人民币 %s' % (time.strftime('%Y-%m-%d %H:%M:%S'), user_card,qx_balance))
          print '%s 账户[%s]取现人民币%s圆' % (time.strftime('%Y-%m-%d %H:%M:%S'), user_card,qx_balance)
        break
      else:
        print '余额不够!'
    else:
      print '输入的格式有误'
  # with open('cardinfo.db','r') as f:
  #   print pickle.load(f)
  # with open('op.log','r') as f:
  #   print pickle.load(f)

def chaBalance(user_dict,user_card):

  print '账户余额为:%s ' % user_dict[user_card]['balance']

def run():
  user_card = panDing()
  print user_card
  while True:
    user_dict=getUser()
    # print '账户余额为:%s ' % user_dict[user_card]['balance']
    choose_num=raw_input('请确认操作:(转账请按1,取现请按 2,余额查询请按3,退出请按4):')
    if re.match('[1234]',choose_num):#根据用户选择类型判断执行方法
      if re.match('[1234]',choose_num).group() == '1':#转帐
        zhuanZhang(user_card)
      elif re.match('[1234]',choose_num).group() == '2':#取现
        quXian(user_card)
      elif re.match('[1234]',choose_num).group() == '3':#余额查询
        chaBalance(user_dict, user_card)
      else:#退出
        break
if __name__ == '__main__':
  run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。