- Python面向對象編程:構建游戲和GUI
- (美)艾維·卡爾布
- 821字
- 2023-06-29 17:17:45
1.2.3 實現(xiàn)2:使用函數(shù)的單個賬戶
在代碼清單1-3對應的版本中,將代碼拆分為單獨的函數(shù),每個函數(shù)對應一種操作。這里仍然只模擬了一個賬戶。
代碼清單1-3:使用函數(shù)的只包含一個賬戶的銀行模擬程序(文件: Bank2_OneAccount WithFunctions.py)
# Non-OOP
# Bank 2
# Single account
accountName = ‘’
accountBalance = 0
accountPassword = ‘’
? def newAccount(name, balance, password):
global accountName, accountBalance, accountPassword
accountName = name
accountBalance = balance
accountPassword = password
def show():
global accountName, accountBalance, accountPassword
print(‘ Name’, accountName)
print(‘ Balance:’, accountBalance)
print(‘ Password:’, accountPassword)
print()
? def getBalance(password):
global accountName, accountBalance, accountPassword
if password != accountPassword:
print(‘Incorrect password’)
return None
return accountBalance
? def deposit(amountToDeposit, password):
global accountName, accountBalance, accountPassword
if amountToDeposit < 0:
print(‘You cannot deposit a negative amount!’)
return None
if password != accountPassword:
print(‘Incorrect password’)
return None
accountBalance = accountBalance + amountToDeposit
return accountBalance
? def withdraw(amountToWithdraw, password):
? global accountName, accountBalance, accountPassword
if amountToWithdraw < 0:
print(‘You cannot withdraw a negative amount’)
return None
if password != accountPassword:
print(‘Incorrect password for this account’)
return None
if amountToWithdraw > accountBalance:
print(‘You cannot withdraw more than you have in your account’)
return None
? accountBalance = accountBalance - amountToWithdraw
return accountBalance
newAccount(“Joe”, 100, ‘soup’) # create an account
while True:
print()
print(‘Press b to get the balance’)
print(‘Press d to make a deposit’)
print(‘Press w to make a withdrawal’)
print(‘Press s to show the account’)
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:’)
userPassword = input(‘Please enter the password: ‘)
theBalance = getBalance(userPassword)
if theBalance is not None:
print(‘Your balance is:’, theBalance)
? elif action == ‘d’:
print(‘Deposit:’)
userDepositAmount = input(‘Please enter amount to deposit: ‘)
userDepositAmount = int(userDepositAmount)
userPassword = input(‘Please enter the password: ‘)
? newBalance = deposit(userDepositAmount, userPassword)
if newBalance is not None:
print(‘Your new balance is:’, newBalance)
--- snip calls to appropriate functions ---
print(‘Done’)
在這個版本中,為銀行賬戶的每個操作(創(chuàng)建賬戶(?)、查詢余額(?)、存款(?)和取款(?))分別創(chuàng)建了一個函數(shù),并重新組織代碼,使主代碼調用不同的函數(shù)。
這樣一來,主程序變得易讀了許多。例如,如果用戶輸入d,表示他們想要存款(?),主代碼現(xiàn)在會調用一個名為deposit()的函數(shù)(?),并傳入存款數(shù)目,以及用戶輸入的賬戶密碼。
但是,如果查看這里的任何函數(shù)(如withdraw()函數(shù))的定義,就會發(fā)現(xiàn)代碼使用了global語句(?)來訪問(獲取或設置)代表賬戶的變量。在Python中,只有當想要在函數(shù)中修改一個全局變量的值時,才需要使用global語句。但是,這里使用它們,只是為了清楚地表明,這些函數(shù)引用了全局變量(盡管只獲取它們的值)。
作為一般編程原則,函數(shù)絕不應該修改全局變量。函數(shù)只應該使用傳遞給它的數(shù)據(jù),基于這些數(shù)據(jù)進行計算,然后返回結果(當然,不必返回結果)。這個程序中的withdraw()函數(shù)確實可以正常運行,但違反了這個原則,它不僅修改了全局變量accountBalance的值(?),還訪問了全局變量accountPassword的值。
推薦閱讀
- Google Flutter Mobile Development Quick Start Guide
- Visual Basic程序開發(fā)(學習筆記)
- Game Programming Using Qt Beginner's Guide
- 青少年軟件編程基礎與實戰(zhàn)(圖形化編程三級)
- Web開發(fā)的貴族:ASP.NET 3.5+SQL Server 2008
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊)
- INSTANT Sencha Touch
- Spring Cloud、Nginx高并發(fā)核心編程
- 程序員修煉之道:通向務實的最高境界(第2版)
- Oracle從入門到精通(第5版)
- C語言程序設計教程
- 從零開始學C#
- Data Science Algorithms in a Week
- 微服務設計
- 現(xiàn)代JavaScript編程:經典范例與實踐技巧