Python模擬自動存取款機的查詢、存取款、修改密碼等操作
1.工作流程

2.模擬自動存取款機的操作
代碼如下:
import msvcrt, sys, os
#定義用星號隱藏密碼輸入的函數(shù)
def psw_input():
li = []
while True:
ch = msvcrt.getch()
#回車
if ch == b'\r':
msvcrt.putch(b'\n')
break
#退格
elif ch == b'\x08':
if li:
li.pop()
msvcrt.putch(b'\b')
msvcrt.putch(b' ')
msvcrt.putch(b'\b')
#Esc
elif ch == b'\x1b':
break
else:
li.append(ch)
msvcrt.putch(b'*')
return li
#定義CSDN銀行ATM歡迎界面的函數(shù)
def ATM():
'''
CSDN銀行ATM歡迎界面的函數(shù)
'''
print("="*14,"Bank of CSDN","="*14,"\n")
print("{:^42}".format("ATM"),"\n")
print("="*14,"Bank of CSDN","="*14,"\n")
#CSDN銀行用戶列表信息,用戶信息包含:姓名、余額、密碼(6位)、銀行卡號(19位)
user_list = [{"name":"張 三","balance":10000,"password":"000000","numbers":"0000000000000000000"},
{"name":"李 四","balance":20000,"password":"111111","numbers":"1111111111111111111"},
{"name":"王 五","balance":30000,"password":"222222","numbers":"2222222222222222222"}]
#定義驗證銀行卡號與密碼匹配的函數(shù)
def check(user_name,user_password):
'''
驗證銀行卡號與密碼匹配的函數(shù)
'''
for i in range(len(user_list)):
if user_name == user_list[i]["numbers"] and user_password == user_list[i]["password"]:
return i #銀行卡號與密碼匹配則返回該用戶在ATM系統(tǒng)內的ID值,否則返回None值
#定義用戶登錄成功后操作界面的函數(shù)
def interface():
'''
用戶登錄成功后操作界面的函數(shù)
'''
print("="*14,"用戶操作界面","="*14,"\n")
print("{0:2} {1:12} {2:12} {3:12}".format(" ","1. 查詢","2. 取款","3. 存款"),"\n")
print("{0:2} {1:10} {2:12}".format(" ","4. 修改密碼","5. 退出"),"\n")
print("="*42,"\n")
#定義用戶查詢信息的函數(shù)
def inquire(user_id):
'''
用戶查詢信息的函數(shù)
'''
print("="*14,"賬號查詢界面","="*14,"\n")
print("|{0:<4}|{1:<18}|{2:<9}|\n".format("賬戶名","卡號","余額(RMB)"))
print("|{0:<5}|{1:<20}|{2:<11}|\n".format(user_list[user_id]["name"],user_list[user_id]["numbers"],user_list[user_id]["balance"]))
#定義用戶取款的函數(shù)
def withdrawal(amount):
'''
用戶取款的函數(shù)
'''
i = user_list[user_id]["balance"]-int(amount)
if i>=0:
user_list[user_id]["balance"]-=int(amount)
else:
print("賬戶余額不足\n")
#定義用戶存款的函數(shù)
def deposit(amount):
'''
用戶存款的函數(shù)
'''
user_list[user_id]["balance"]+=int(amount)
#定義用戶修改密碼的函數(shù)
def change_password(old_password,new_password1,new_password2):
'''
用戶修改密碼的函數(shù)
'''
if old_password == user_list[user_id]["password"]:
if new_password1 == new_password2:
user_list[user_id]["password"] = new_password1
print("新密碼修改成功\n")
return 1
else:
print("修改密碼失敗,您2次輸入的新密碼不一致\n")
return 2
else:
print("舊密碼輸入錯誤\n")
return 0
#用戶登錄界面,輸入銀行卡號和密碼
chance = 3 #允許3次用戶名或密碼輸入錯誤
while True:
ATM()
user_name = input("請輸入您的銀行卡卡號:")
print("")
print("請輸入您的銀行卡密碼:", end=' ', flush=True)
user_password = b''.join(psw_input()).decode()
print("")
user_id = check(user_name,user_password)#驗證銀行卡號與密碼是否匹配
if user_id != None:
print("登錄成功\n")
while True:
interface()
key_word = input("請輸入操作選項:")
print("")
if key_word == "1":
inquire(user_id)
input("按任意鍵返回上一級目錄:")
print("")
elif key_word == "2":
print("="*14,"賬號取款界面","="*14,"\n")
amount = input("請輸入取款金額:")
print("")
withdrawal(amount)
inquire(user_id)
input("按任意鍵返回上一級目錄:")
print("")
elif key_word == "3":
print("="*14,"賬號存款界面","="*14,"\n")
amount = input("請輸入存款金額:")
print("")
deposit(amount)
inquire(user_id)
input("按任意鍵返回上一級目錄:")
print("")
elif key_word == "4":
print("="*14,"密碼管理界面","="*14,"\n")
print("請輸入舊密碼:", end=' ', flush=True)
old_password = b''.join(psw_input()).decode()
print("")
print("請輸入新密碼:", end=' ', flush=True)
new_password1 = b''.join(psw_input()).decode()
print("")
print("請再次輸入新密碼:", end=' ', flush=True)
new_password2 = b''.join(psw_input()).decode()
print("")
save = change_password(old_password,new_password1,new_password2)
#如何檢測到舊密碼輸入有誤,將直接退出
if save == 0:
break
elif key_word == "5":
print("="*14,"歡迎下次光臨","="*14,"\n")
break
else:
print("="*14,"沒有該選項","="*14,"\n")
else:
if chance > 1:
print("用戶名或密碼錯誤,您還有",chance-1,"次機會,請重新輸入\n")
chance -= 1
else:
print("對不起,您輸入用戶名或密碼錯誤已達3次")
break
3.運行結果
有以下初始用戶信息備測試用:
姓名 銀行卡號(19位) 密碼(6位) 余額(RMB)
張 三 0000000000000000000 000000 10000
李 四 1111111111111111111 111111 20000
王 五 2222222222222222222 222222 30000
輸入卡號和密碼進入用戶操作界面
查詢余額界面
取款界面
存款界面
修改密碼界面
總結
以上所述是小編給大家介紹的Python模擬自動存取款機的查詢、存取款、修改密碼等操作,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
CentOS 7下Python 2.7升級至Python3.6.1的實戰(zhàn)教程
Centos是目前最為流行的Linux服務器系統(tǒng),其默認的Python 2.x,這篇文章主要給大家分享了關于在CentOS 7下Python 2.7升級至Python3.6.1的實戰(zhàn)教程,文中將升級的步驟一步步的介紹的非常詳細,對大家的理解和學習具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07
解決ImportError:cannot import name ‘Flatten‘&nb
這篇文章主要介紹了解決ImportError:cannot import name ‘Flatten‘ from ‘torch.nn‘問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
使用Python判斷質數(shù)(素數(shù))的簡單方法講解
這篇文章主要介紹了使用Python判斷質數(shù)(素數(shù))的簡單方法講解,經常被用來做科學計算的Python處理這種小問題當然手到擒來^_-需要的朋友可以參考下2016-05-05

