Bank transfer, Python Programming

Bank transfer

What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is shown as a list of values,  such as ['Alyssa', 8300343.03, 0.05], defining that 'Alyssa' has a bank balance  of $8,300,343.03, and has to give a 5-cent fee for every  bank transaction. We may  give  this function as follows. It transfers the amount from  one balance  to the other,  and  deduct the transaction fee from  each account.

def transfer(a1, a2, amt):

a1[1] = a1[1] - amt - a1[2]

a2[1] = a2[1] + amt - a2[2]

To understand what  it's doing,  you really have to read  the code at a detailed layer.  Furthermore, it's simple to get the variable names  and subscripts wrong.

 

Here's another version that  abstracts away  the common idea of a deposit (which  can be positive or negative) into a function, and uses it twice:

 

def transfer(a1, a2, amt): deposit(a1, -amt) deposit(a2, amt)

def deposit(a, amt):

a[1] = a[1] + amt - a[2]

 

Now,  transfer looks pretty simple, but deposit can  still use some work.  In particular, the need of numeric indices  to get the components out of the bank  account de?nition is a bit cryptic  (and easy to get wrong).10

 

def deposit(a, amt):

(name, balance, fee) = a

a[1] = balance + amt - fee

 

Here,  we've used  a destructuring assignment statement to give names  to the components of the account. Unfortunately, when  we want  to modify  a component of the list showing the account, we have  to index  it explicitly.   Shown  that  we have  to use explicit  indices,  this  mechanism in which  we name  them  might  be better.

 

acctName = 0 acctBalance = 1 acctFee = 2

def deposit(a, amt):

a[acctBalance] = a[acctBalance] + amt - a[acctFee]

 

Strive, in your  programming, to make  your  code as easy, simple, clear and  direct  as possible.  Rarely,  the clear  and  simple approach will be too inef?cient, and  you'll have  to work on  something more  hard. In such type, you should still initiate with  something simple and  clear,  and  in the end, you may use it as documentation

 

Posted Date: 8/9/2012 7:24:51 AM | Location : United States







Related Discussions:- Bank transfer, Assignment Help, Ask Question on Bank transfer, Get Answer, Expert's Help, Bank transfer Discussions

Write discussion on Bank transfer
Your posts are moderated
Related Questions
Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

For this assignment, you need to create a program that allows the user to do basic trigonometry functions. First, ask the user if he or she would like to do sine, cosine, or tangen

In English, specify a representation of the board game in Python. The representation should capture the entire state of the game at one point in time. It may be helpful to first fi

Bank transfer What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is show

protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

Primitives, Composition, Abstraction, and Patterns   We will  start  by thinking about  how  the  PCAP  framework applies to computer programs, in general. We may do that by

Booleans   Before we talk about  conditionals, we require  to clarify the Boolean  data  type.  It has two values False and True. Typical statement that have Boolean values

python program of motion of a particle in viscus medium

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl