小編給大家分享一下如何使用Python實現ATM系統,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

案例剖析:
1.atm主頁面顯示.
2.賬號驗證.
3.查詢余額.
4.存款.
5.取款.
6.修改密碼.
ps:我的思路是將以上的功能分成幾大塊,通過函數,循環和返回值來實現,話不多說直接上代碼.
代碼:
1.atm主頁面顯示
def index(nowUser):
 print('=' * 10, '自動存取款機', '=' * 10)
 print(getName(nowUser), '歡迎登陸!')
 print('{:<10}{:^10}{:<10}'.format('1.修改密碼', ' ', '2.查詢余額'))
 print('{:<10}{:^12}{:<10}'.format('3.存錢', ' ', '4.取錢'))
 print('輸入\'exit\'退出操作')ps:使用format來進行格式的定義.
2.賬號驗證
def getId(nowUser): '''獲取當前用戶在列表中的位置''' index = -1 for i in range(len(userList)): if userList[i]['cardid'] == nowUser: index = i # 如果用戶存在則返回它在列表中的下標 break return index # 如果用戶不存在則返回-1 def getName(nowUser): ''' 獲取用戶姓名''' index = getId(nowUser) return userList[index]['name'] def checkUser(cardid, password): '''自定義用戶檢測功能,包括卡號及密碼檢測''' index = getId(cardid) if index == -1: # 如果用戶不存在 return 'noCardId' # 卡號不存在 else: if userList[index]['cardid'] == cardid and userList[index]['password'] == password: # 用戶存在并且賬號密碼正確 return 'login' # 密碼正確 else: return 'errorPW' # 密碼錯誤
ps:使用循環和返回值來進行賬號的對比和檢測.
3.查詢余額
def showMoney(nowUser):
 '''查詢余額'''
 index = getId(nowUser)
 print('您當前的賬戶余額為:', userList[index]['money'], '元')4.存錢
def saveMoney(nowUser, money):
 '''存錢'''
 index = getId(nowUser)
 print("輸入的金額是:", money)
 userList[index]['money'] += int(money)
 print('存入成功!')5.取錢
def drawMoney(nowUser, money):
 '''取錢'''
 index = getId(nowUser)
 nowMoney = userList[index]['money']
 if nowMoney >= int(money):
  userList[index]['money'] -= int(money)
  print('已取出', money, '元')
 else:
  print('賬戶余額不足!')6.修改密碼
def changePW(nowUser, newPW): '''修改密碼''' index = getId(nowUser) userList[index]['password'] = newPW
7.定義用戶操作函數
# 用戶操作
def userChoice(nowUser):
 choices = ['1', '2', '3', '4', 'exit']
 # 循環獲取用戶操作
 while True:
  index(nowUser)
  choice = input('請選擇操作:')
  if choice == '1': # 選擇修改密碼
   oldPW = input('請輸入原始密碼:')
   flag = checkUser(nowUser, oldPW)
   if flag == "errorPW":
    # print('密碼錯誤!請重新輸入,或輸入\'back\'返回上一級')
    print('密碼錯誤!返回主界面')
   elif flag == 'login':
    changePW(nowUser, input('請輸入新密碼:'))
    print('修改密碼成功!')
    continue
  elif choice == '2': # 選擇查詢余額
   showMoney(nowUser)
   continue
  elif choice == '3': # 選擇存錢
   saveMoney(nowUser, input('請輸入存入金額:'))
   continue
  elif choice == '4': # 選擇取錢
   drawMoney(nowUser, input('請輸入取出金額:'))
   continue
  elif choice == 'exit':
   main() # 返回主界面
  elif choice not in choices:
   print('錯誤操作,請重新輸入選項!')
   continue8.定義main主函數
# 主界面
def main():
 # 定義錯誤次數
 errorTime = 0
 if errorTime >= 3: # 錯誤次數達三次退出系統后清零
  errorTime = 0
 while True:
  # crs登陸主界面
  print('=' * 10, '自動存取款機', '=' * 10)
  nowUser = ''
  # 用戶輸入卡號和密碼
  cardid = input('請輸入卡號:')
  password = input('請輸入密碼:')
  # 判斷卡號密碼是否存在正確
  # 卡號不存在
  flag = checkUser(cardid, password)
  if flag == 'noCardId':
   print('卡號不存在!請重新輸入')
   continue
  # 密碼錯誤
  elif flag == 'errorPW':
   errorTime += 1
   print('密碼錯誤!錯誤次數達三次將自動退出本系統!')
   print("錯誤次數:", errorTime)
   if errorTime >= 3: # 錯誤次數達三次自動退出
    print('密碼輸錯三次,自動退出系統!')
    exit(0)
   continue
  # 卡號密碼正確進入系統
  else:
   nowUser = cardid
   userChoice(nowUser)9.最后定義函數主入口,進行ATM系統的測試
if __name__ == '__main__': main()
收獲:
通過ATM案例 ,將之前學習的Python基礎重新進行了一次鞏固,梳理和融會貫通,個人感覺 Python的函數和返回值是個特別神奇的東西,當然這個案例還有很大的改進空間,大家有什么好的建議也可以給我留言,我之后會慢慢進行改良噠!
以上是“如何使用Python實現ATM系統”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯成都網站設計公司行業資訊頻道!
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
                網頁標題:如何使用Python實現ATM系統-創新互聯
                
                標題鏈接:http://www.yijiale78.com/article36/docdsg.html
            
成都網站建設公司_創新互聯,為您提供自適應網站、ChatGPT、外貿網站建設、App開發、品牌網站建設、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯
