Python遊戲編程(十六)模擬自動取款機

模擬自動取款機,數據臨時存放在變量列表中,實現一個取款機上的存取款模擬效果,包括登錄、退出、查詢餘額、取款和存款功能。

目錄

(一)設置賬號信息列表

(二)showAccout(accout,passwd,accoutList):

format方法:

(三)showLoginPage():

(四)checkAccout(accout, passwd):

 

(五)showMainPage():

(六)atmService(index):

(七)開始循環


這裏編寫一個Python程序,來實現以上功能,當然僅僅是通過一些文本來表示。

(一)設置賬號信息列表

對於一個類似銀行、應用用戶羣體這樣龐大的數據羣體,多數是採用數據庫進行管理,當然對於數據庫我們也需要有一定的瞭解之後,才能使用。這裏先用一個列表存儲一些數據:

# 賬號信息列表
accoutList = [{'accout':'442018001','name':'Allen','passwd':'123456','balance':100.00},
              {'accout':'442018002','name':'Bonus','passwd':'abcdef','balance':100.00},
              {'accout':'442018003','name':'Carrie','passwd':'hello','balance':200.00},
              {'accout':'442018004','name':'Bob','passwd':'hello','balance':400.00}]

 

(二)showAccout(accout,passwd,accoutList):

自定義showAccout()函數實現管理員功能,這裏設置了管理員模式下可以查看所有用戶的個人信息:

# 進入管理員系統,顯示所有賬戶信息
def showAccout(accout,passwd,accoutList):
    if accout == "1" and passwd == "1":
        if len(accoutList) == 0:
            print("(I) No Data from Accout")
            return
        print("|{0:<10} | {1:<10} | {2:<10} | {3:<10}".format("accout",
            "name","passwd","balance"))
        print("-"*40)
        for i in range(len(accoutList)):
            print("|{0:<10} | {1:<10} | {2:<10} | {3:<10}".format(accoutList[i]['accout'],
                accoutList[i]['name'],"******",accoutList[i]['balance']))
        print("")
        input("按回車鍵退出管理員系統:")
        return
    else:
        return None

format方法:

format方法是一種強大的字符串格式化腳本,稱得上是一種微型語言,幾乎爲操作字符串提供了無限可能。不過這裏只是簡單介紹,更多的瞭解參見標準庫文檔的字符串格式部分。

除了使用format方法格式化字符串,還可以使用%格式化字符串。

 

(三)showLoginPage():

涉資一個文本登錄界面:

# 登錄界面
def showLoginPage():
    print("+-------------------------------+")
    print("+                               +")
    print("+              ATM              +")
    print("+                               +")
    print("+-------------------------------+")
    print("            (請插卡)           ")

 

(四)checkAccout(accout, passwd):

 

# 驗證賬號及密碼
def checkAccout(accout, passwd):   
    for i in range(len(accoutList)):
        if accout == accoutList[i]['accout'] and passwd == accoutList[i]['passwd']:
            return i
    # 沒有匹配成功
    return -1

 

(五)showMainPage():

 

# 初始化界面
def showMainPage():

    print("")
    print("="*13, "自動存取款系統", "="*13)
    print("{0:1} {1:13} {2:15}".format(" ", "1.查詢",
        "2.取款"))
    print("{0:1} {1:13} {2:15}".format(" ", "3.存款",
        "4.退出系統")) 
    print("="*42)

 

(六)atmService(index):

 

# 自動取款機業務
def atmService(index):
    key = input("請輸入對應的選擇:")
    print("")
    if key == "1":
        print("="*14, "查詢賬戶餘額", "="*14)
        #showAccout(accoutList)
        print("> 賬戶姓名:", accoutList[index]['name'])
        print("> 當前賬戶:", accoutList[index]['accout'])
        print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
        return 0

    elif key == "2":
        print("="*14, "取款", "="*14)
        money = input("請輸入取款金額(元):¥")
        if int(money) > 0 and int(money) <= accoutList[index]['balance']:
            accoutList[index]['balance'] -= int(money)
            print("> 取款成功!*^_^*")
            print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
            print("(I) 請取走現金並妥善保管")
            return 0
        else:
            print("(E) 餘額不足,取款失敗")
            return -1

    elif key == "3":
        print("="*14, "存款", "="*14)
        money = input("> 存入金額(元):¥")
        accoutList[index]['balance'] += int(money)
        print("> 存款成功!*^_^*")
        print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
        return 0

    elif key == "4":
        print("="*18, "再見", "="*18)
        return 1
    else:
        print("Try again!")
        return -1

 

 

(七)開始循環

 

# 開始啦
showLoginPage()
accout = input("請輸入賬號:")
passwd = input("請輸入密碼:")
showAccout(accout,passwd,accoutList)
index = checkAccout(accout, passwd)
if index >= 0:
    print("(I) 登錄成功")
    while True:
        showMainPage()
        if 1 == atmService(index):
            break
        input("按回車鍵繼續:")
else:
    print("(I) 登錄失敗,請檢查賬號和密碼")

 

 

源代碼:

 

'''
 Name: 模擬自動取款機
 Athor: 盧華東
 Date: 2018-06-18
 Description: 數據臨時存放在變量列表中,實現一個取款機上的存取款
              模擬效果,包括登錄、退出、查詢餘額、取款和存款功能
'''

# 賬號信息列表
accoutList = [{'accout':'442018001','name':'Allen','passwd':'123456','balance':100.00},
              {'accout':'442018002','name':'Bonus','passwd':'abcdef','balance':100.00},
              {'accout':'442018003','name':'Carrie','passwd':'hello','balance':200.00},
              {'accout':'442018004','name':'Bob','passwd':'hello','balance':400.00}]

# 進入管理員系統,顯示所有賬戶信息
def showAccout(accout,passwd,accoutList):
    if accout == "1" and passwd == "1":
        if len(accoutList) == 0:
            print("(I) No Data from Accout")
            return
        print("|{0:<10} | {1:<10} | {2:<10} | {3:<10}".format("accout",
            "name","passwd","balance"))
        print("-"*40)
        for i in range(len(accoutList)):
            print("|{0:<10} | {1:<10} | {2:<10} | {3:<10}".format(accoutList[i]['accout'],
                accoutList[i]['name'],"******",accoutList[i]['balance']))
        print("")
        input("按回車鍵退出管理員系統:")
        return
    else:
        return None
        
    

# 登錄界面
def showLoginPage():
    print("+-------------------------------+")
    print("+                               +")
    print("+              ATM              +")
    print("+                               +")
    print("+-------------------------------+")
    print("            (請插卡)           ")


# 驗證賬號及密碼
def checkAccout(accout, passwd):   
    for i in range(len(accoutList)):
        if accout == accoutList[i]['accout'] and passwd == accoutList[i]['passwd']:
            return i
    # 沒有匹配成功
    return -1



# 初始化界面
def showMainPage():

    print("")
    print("="*13, "自動存取款系統", "="*13)
    print("{0:1} {1:13} {2:15}".format(" ", "1.查詢",
        "2.取款"))
    print("{0:1} {1:13} {2:15}".format(" ", "3.存款",
        "4.退出系統")) 
    print("="*42)

# 自動取款機業務
def atmService(index):
    key = input("請輸入對應的選擇:")
    print("")
    if key == "1":
        print("="*14, "查詢賬戶餘額", "="*14)
        #showAccout(accoutList)
        print("> 賬戶姓名:", accoutList[index]['name'])
        print("> 當前賬戶:", accoutList[index]['accout'])
        print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
        return 0

    elif key == "2":
        print("="*14, "取款", "="*14)
        money = input("請輸入取款金額(元):¥")
        if int(money) > 0 and int(money) <= accoutList[index]['balance']:
            accoutList[index]['balance'] -= int(money)
            print("> 取款成功!*^_^*")
            print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
            print("(I) 請取走現金並妥善保管")
            return 0
        else:
            print("(E) 餘額不足,取款失敗")
            return -1

    elif key == "3":
        print("="*14, "存款", "="*14)
        money = input("> 存入金額(元):¥")
        accoutList[index]['balance'] += int(money)
        print("> 存款成功!*^_^*")
        print("> 當前餘額:", accoutList[index]['balance'], end='\n\n')
        return 0

    elif key == "4":
        print("="*18, "再見", "="*18)
        return 1
    else:
        print("Try again!")
        return -1


# 開始啦
showLoginPage()
accout = input("請輸入賬號:")
passwd = input("請輸入密碼:")
showAccout(accout,passwd,accoutList)
index = checkAccout(accout, passwd)
if index >= 0:
    print("(I) 登錄成功")
    while True:
        showMainPage()
        if 1 == atmService(index):
            break
        input("按回車鍵繼續:")
else:
    print("(I) 登錄失敗,請檢查賬號和密碼")

對於數據的處理,最好的方式就是用數據庫了。python提供了pygame模塊用來連接數據庫,這裏只是簡單使用一個列表來臨時存儲賬戶信息,隨着編譯器的關閉,數據也就被清除了。要想讓數據留下來,可以使用數據庫。後面我會嘗試使用數據庫來存儲數據。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章