- Python面向對象編程:構建游戲和GUI
- (美)艾維·卡爾布
- 943字
- 2023-06-29 17:17:47
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ù)。
推薦閱讀
- Java加密與解密的藝術(第2版)
- 基于Java技術的Web應用開發(fā)
- Processing互動編程藝術
- 教孩子學編程:C++入門圖解
- Troubleshooting PostgreSQL
- JavaScript:Moving to ES2015
- 用戶體驗增長:數(shù)字化·智能化·綠色化
- Extreme C
- 計算機應用基礎項目化教程
- Fastdata Processing with Spark
- Python程序設計開發(fā)寶典
- Python Programming for Arduino
- 寫給青少年的人工智能(Python版·微課視頻版)
- Java EE項目應用開發(fā)
- iOS Development with Xamarin Cookbook