您好,登錄后才能下訂單哦!
1.工作流程
2.模擬自動存取款機(jī)的操作
代碼如下:
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"}] #定義驗(yàn)證銀行卡號與密碼匹配的函數(shù) def check(user_name,user_password): ''' 驗(yàn)證銀行卡號與密碼匹配的函數(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)內(nèi)的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)#驗(yàn)證銀行卡號與密碼是否匹配 if user_id != None: print("登錄成功\n") while True: interface() key_word = input("請輸入操作選項(xiàng):") 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("請?jiān)俅屋斎胄旅艽a:", 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,"沒有該選項(xiàng)","="*14,"\n") else: if chance > 1: print("用戶名或密碼錯誤,您還有",chance-1,"次機(jī)會,請重新輸入\n") chance -= 1 else: print("對不起,您輸入用戶名或密碼錯誤已達(dá)3次") break
3.運(yùn)行結(jié)果
有以下初始用戶信息備測試用:
姓名 銀行卡號(19位) 密碼(6位) 余額(RMB)
張 三 0000000000000000000 000000 10000
李 四 1111111111111111111 111111 20000
王 五 2222222222222222222 222222 30000
輸入卡號和密碼進(jìn)入用戶操作界面
查詢余額界面
取款界面
存款界面
修改密碼界面
總結(jié)
以上所述是小編給大家介紹的Python模擬自動存取款機(jī)的查詢、存取款、修改密碼等操作,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。