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

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的值。

主站蜘蛛池模板: 福州市| 左云县| 温泉县| 和林格尔县| 体育| 长汀县| 莒南县| 惠水县| 和政县| 绥滨县| 巢湖市| 贵溪市| 万山特区| 禄丰县| 理塘县| 大方县| 凤冈县| 会泽县| 宝兴县| 仁寿县| 曲阜市| 富阳市| 马鞍山市| 崇义县| 北海市| 四川省| 田林县| 滨海县| 绵阳市| 颍上县| 南丹县| 阿巴嘎旗| 堆龙德庆县| 昌江| 吉首市| 南京市| 罗城| 莒南县| 宜州市| 乌恰县| 廊坊市|