1.工作流程
2.模擬自動存取款機的操作
代碼如下:
import msvcrt, sys, os#定義用星號隱藏密碼輸入的函數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歡迎界面的函數def ATM(): ''' CSDN銀行ATM歡迎界面的函數 ''' 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"}]#定義驗證銀行卡號與密碼匹配的函數def check(user_name,user_password): ''' 驗證銀行卡號與密碼匹配的函數 ''' for i in range(len(user_list)): if user_name == user_list[i]["numbers"] and user_password == user_list[i]["password"]: return i #銀行卡號與密碼匹配則返回該用戶在ATM系統內的ID值,否則返回None值#定義用戶登錄成功后操作界面的函數def interface(): ''' 用戶登錄成功后操作界面的函數 ''' 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")#定義用戶查詢信息的函數def inquire(user_id): ''' 用戶查詢信息的函數 ''' 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"]))#定義用戶取款的函數def withdrawal(amount): ''' 用戶取款的函數 ''' i = user_list[user_id]["balance"]-int(amount) if i>=0: user_list[user_id]["balance"]-=int(amount) else: print("賬戶余額不足/n")#定義用戶存款的函數def deposit(amount): ''' 用戶存款的函數 ''' user_list[user_id]["balance"]+=int(amount)#定義用戶修改密碼的函數def change_password(old_password,new_password1,new_password2): ''' 用戶修改密碼的函數 ''' 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
新聞熱點
疑難解答