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']import pickle
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()
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持我们。本⽂标题: python 模拟银⾏转账功能过程详解
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论