首先畫出流程圖,流程圖與現(xiàn)實(shí)代碼有出入,因?yàn)閯傞_(kāi)始畫流程圖的時(shí)候,有些東西沒(méi)考慮進(jìn)去,后來(lái)寫著寫著就慢慢能想起來(lái)并實(shí)現(xiàn)了。
另有一點(diǎn)經(jīng)驗(yàn)推薦給新手朋友,如果說(shuō)碰到一個(gè)項(xiàng)目無(wú)從下手的話,就先慢慢去寫,寫著寫著你就會(huì)往下寫了,真的,親身實(shí)踐。望大神勿噴~
#!/usr/bin/env python
#encoding:utf-8
import re
import pickle
import time
def getUser():
'''從數(shù)據(jù)文件里獲取銀行卡用戶信息'''
with open('cardinfo.db','r') as f:
return pickle.load(f)
def panDing():
'''判定用戶銀行卡信息跟密碼信息的準(zhǔn)確性'''
while True:
user_dict=getUser()
print user_dict
card_num=raw_input('請(qǐng)輸入您的19位銀行卡號(hào)(只包含數(shù)字):')#獲取用戶卡號(hào)
if re.match('\d{19}',card_num) and card_num in user_dict:#判斷卡號(hào)是否匹配
card_passwd=(raw_input('請(qǐng)輸入您的銀行卡密碼:'))
# 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']:#判定密碼對(duì)錯(cuò)
break
else:
print '密碼錯(cuò)誤!'
continue
else:
print '您輸入的銀行卡信息有誤!'
return card_num
def zhuanZhang(srcaccount):
'''用戶轉(zhuǎn)賬操作'''
user_dict = getUser()
while True:
target_account = raw_input('請(qǐng)輸入目標(biāo)賬戶:')
if re.match('\d{19}', target_account) :
if target_account in user_dict: # 判斷卡號(hào)是否匹配
while True:
tr_balance = int(raw_input('請(qǐng)輸入轉(zhuǎn)賬金額:'))
if tr_balance <= user_dict[srcaccount]['balance']:#對(duì)比轉(zhuǎn)賬金額跟賬戶余額
break
else:
print '轉(zhuǎn)賬金額大于余額,請(qǐng)重新輸入余額!'
break
else:
print '卡號(hào)錯(cuò)誤,請(qǐng)重新輸入!'
else:
print '卡號(hào)不對(duì)'
print '轉(zhuǎn)入的賬戶為:%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 '轉(zhuǎn)入的賬戶為:%s ,轉(zhuǎn)入的金額為:%s' % (target_account, tr_balance)
# print '轉(zhuǎn)入賬戶為:%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轉(zhuǎn)入到賬戶%s中%s人民幣' % (time.strftime('%Y-%m-%d %H:%M:%S'),srcaccount,target_account,tr_balance),f)
print '%s 賬戶%s轉(zhuǎn)入到賬戶%s中%s人民幣' % (time.strftime('%Y-%m-%d %H:%M:%S'),srcaccount,target_account,tr_balance)
def quXian(user_card):
'''用戶取現(xiàn)操作'''
user_dict = getUser()
while True:
qx_balance=raw_input('請(qǐng)輸入取現(xiàn)金額:')
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 取現(xiàn)人民幣 %s' % (time.strftime('%Y-%m-%d %H:%M:%S'), user_card,qx_balance))
print '%s 賬戶[%s]取現(xiàn)人民幣%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('請(qǐng)確認(rèn)操作:(轉(zhuǎn)賬請(qǐng)按1,取現(xiàn)請(qǐng)按 2,余額查詢請(qǐng)按3,退出請(qǐng)按4):')
if re.match('[1234]',choose_num):#根據(jù)用戶選擇類型判斷執(zhí)行方法
if re.match('[1234]',choose_num).group() == '1':#轉(zhuǎn)帳
zhuanZhang(user_card)
elif re.match('[1234]',choose_num).group() == '2':#取現(xiàn)
quXian(user_card)
elif re.match('[1234]',choose_num).group() == '3':#余額查詢
chaBalance(user_dict, user_card)
else:#退出
break
if __name__ == '__main__':
run()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

