官术网_书友最值得收藏!

1.2.6 實現(xiàn)5:賬戶字典的列表

為了實現(xiàn)最后這種方法,我們將使用一種稍微復雜一些的數(shù)據(jù)結構。在這個版本中,創(chuàng)建一個賬戶列表,其中每個賬戶(列表中的每個元素)是如下所示的一個字典。

{‘name’:<someName>, ‘password’:<somePassword>, ‘balance’:<someBalance>}

注意:

在本書中,每當我在尖括號(<>)中給出一個值的時候,意味著你應該使用自己選擇的值來替換該項(以及尖括號)。例如,在上面的代碼行中,<someName>、<somePassword>和<someBalance>是占位符,應該用實際值替換它們。

最后,這個實現(xiàn)的代碼如代碼清單1-6所示。

代碼清單1-6:使用字典列表的銀行模擬程序(文件: Bank5_Dictionary.py)

# Non-OOP Bank
# Version 5
# Any number of accounts - with a list of dictionaries
 
accountsList = [] ?
 
def newAccount(aName, aBalance, aPassword):
    global accountsList
    newAccountDict = {‘name’:aName, ‘balance’:aBalance, ‘password’:aPassword}
    accountsList.append(newAccountDict) ?
 
def show(accountNumber):
    global accountsList
    print(‘Account’, accountNumber)
    thisAccountDict = accountsList[accountNumber]
    print(‘       Name’, thisAccountDict[‘name’])
    print(‘       Balance:’, thisAccountDict[‘balance’])
    print(‘       Password:’, thisAccountDict[‘password’])
    print()
 
def getBalance(accountNumber, password):
    global accountsList
    thisAccountDict = accountsList[accountNumber] ?
    if password != thisAccountDict[‘password’]:
        print(‘Incorrect password’)
        return None
    return thisAccountDict[‘balance’]
 
--- snipped additional deposit() and withdraw() functions ---
 
# Create two sample accounts
print(“Joe’s account is account number:”, len(accountsList))
newAccount(“Joe”, 100, ‘soup’)
 
print(“Mary’s account is account number:”, len(accountsList))
newAccount(“Mary”, 12345, ‘nuts’)
 
 
while True:
    print()
    print(‘Press b to get the balance’)
    print(‘Press d to make a deposit’)
    print(‘Press n to create a new account’)
    print(‘Press w to make a withdrawal’)
    print(‘Press s to show all accounts’)
    print(‘Press q to quit’)
    print()
 
    action = input(‘What do you want to do? ‘)
    action = action.lower() # force lowercase
    action = action[0] # just use first letter
    print()
 
    if action == ‘b’:
        print(‘Get Balance:’)
        userAccountNumber = input(‘Please enter your account number: ‘)
        userAccountNumber = int(userAccountNumber)
        userPassword = input(‘Please enter the password: ‘)
        theBalance = getBalance(userAccountNumber, userPassword)
        if theBalance is not None:
            print(‘Your balance is:’, theBalance)
 
    elif action == ‘d’:
        print(‘Deposit:’)
        userAccountNumber= input(‘Please enter the account number: ‘)
        userAccountNumber = int(userAccountNumber)
        userDepositAmount = input(‘Please enter amount to deposit: ‘)
        userDepositAmount = int(userDepositAmount)
        userPassword = input(‘Please enter the password: ‘)
 
        newBalance = deposit(userAccountNumber, userDepositAmount, userPassword)
        if newBalance is not None:
            print(‘Your new balance is:’, newBalance)
 
    elif action == ‘n’:
        print(‘New Account:’)
        userName = input(‘What is your name? ‘)
        userStartingAmount = input(‘What is the amount of your initial deposit? ‘)
        userStartingAmount = int(userStartingAmount)
        userPassword = input(‘What password would you like to use for this account? ‘)
 
        userAccountNumber = len(accountsList)
        newAccount(userName, userStartingAmount, userPassword)
        print(‘Your new account number is:’, userAccountNumber)
 
--- snipped additional user interface ---
 
print(‘Done’)

使用這種方法,可以在一個字典中找到與一個賬戶相關的所有數(shù)據(jù)(?)。要創(chuàng)建一個新賬戶,我們創(chuàng)建一個新的字典,將其追加到賬戶列表中(?)。為每個賬戶分配一個數(shù)字(一個簡單的整數(shù)),對賬戶執(zhí)行任何操作時,都必須提供這個賬號。例如,當用戶存款時,需要提供自己的賬號,getBalance()函數(shù)會使用該賬號作為賬戶列表中的索引(?)。

這種方法讓代碼整潔了許多,使數(shù)據(jù)的組織更加符合邏輯。但是,程序中的每個函數(shù)仍然必須訪問全局賬戶列表。讓函數(shù)能夠訪問所有賬戶數(shù)據(jù)會帶來潛在的安全風險。理想情況下,每個函數(shù)只應該能夠影響一個賬戶的數(shù)據(jù)。

主站蜘蛛池模板: 桂林市| 北海市| 化德县| 淳化县| 钦州市| 周至县| 麻城市| 武乡县| 南乐县| 根河市| 富平县| 平定县| 阳新县| 丹棱县| 南充市| 镇平县| 景洪市| 宁河县| 锡林浩特市| 阿鲁科尔沁旗| 龙川县| 宁远县| 龙山县| 平乡县| 汉阴县| 玛沁县| 康定县| 子长县| 临沂市| 巩义市| 宁武县| 鹤山市| 泌阳县| 岗巴县| 克山县| 新宁县| 宁波市| 清原| 塘沽区| 灵璧县| 明水县|