Bank transfer, Python Programming

Assignment Help:

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

 


Related Discussions:- Bank transfer

Small Viruses, I need how to write small kind of viruses...

I need how to write small kind of viruses...

Basics of python-introduction, Basics of python-introduction Python is...

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

Non-local references, Non-local references So far, whenever we needed ...

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

Algorithms, Write an algorithm for the sum of the given series 1,-1/2,1/4,-...

Write an algorithm for the sum of the given series 1,-1/2,1/4,-1/8.....

Types and declarations, Java programs are  what  is known as statically an...

Java programs are  what  is known as statically and  strongly defined.  Thus,  the  kinds  of all the variables must  be known at the time that the program is written. That seems

Cps translator for python, Your task is to construct a translator from the ...

Your task is to construct a translator from the subset of Python in Project 3 intocontinuation-passing style (CPS). Of course, the expected way of accomplishing this is to trans

Program to solve word search puzzles, This assignment involves writing a pr...

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Re-writing a C++ code in Python with the help of ctypes, I have a C++ code ...

I have a C++ code (10-15 line) which i need to re-write in python with the help of ctypes library. Is it possible i can get some help with it? Thanks & regards Tanmoy

Simple expressions, Simple expressions A cornerstone of a programming ...

Simple expressions A cornerstone of a programming language is the  ability  to compute expressions.  We will start here  with  arithmetic expressions, just to take the  concep

Structured assignment, Structured assignment Once  we have  tuples and...

Structured assignment Once  we have  tuples and lists, we may use  a nice trick  in assignment expression, based  on the packing and unpacking of tuples. >>> a, b, c = 1, 2

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd