ADVERTISEMENT
ADVERTISEMENT

# Python | Compute the net amount of a bank account based on the transactions

Here, we are implementing a Python program that will be used to calculate Net amount of a bank account based on the transactions.
Submitted by IncludeHelp, on August 08, 2018

Given a few transactions (deposit, withdrawal), and we have to compute the Net Amount of that bank account based on these transactions in Python.

Example:

```    Input:
Enter transactions: D 10000
Want to continue (Y for yes): Y
Enter transaction: W 5000
Want to continue (Y for yes): Y
Enter transaction: D 2000
Want to continue (Y for yes): Y
Enter transaction: W 100
Want to continue (Y for yes): N

Output:
Net amount: 6900
```

Logic:

• For this program, we will input transactions (till user’s choice is "Y") with type ("D" for deposit and "W" for withdrawal) and the amount – for that we will computer it in an infinite loop while True:
• Transactions will be like "D 2000" that means Deposit 2000, so there are two values needs to be extracted 1) "D" is the type of transaction and 2) 2000 is the amount to be deposited.
• A transaction input will be string type- convert/split the values to the list which is delimited by the space. For this, we use string.split() method.
• Now, the values will be in the list, the first value is the type – which is in string format and the second value is the amount – which is in also string format, we need to convert the amount in integer format. So we will convert its type (example code: amount = int(list[1])).
• After that, based on the transaction type check the conditions and adding/subtracting the amount from the net amount.
• After that, ask for the user for the next transaction and check the weather, if the user’s choice is not "Y" or "y", break the loop and print the net amount.

Program:

```# computes net bank amount based on the input
# "D" for deposit, "W" for withdrawal

# define a variable for main amount
net_amount = 0

while True:
# input the transaction
str = raw_input ("Enter transaction: ")

# get the value type and amount to the list
# seprated by space
transaction = str.split(" ")

# get the value of transaction type and amount
# in the separated variables
type = transaction [0]
amount = int (transaction [1])

if type=="D" or type=="d":
net_amount += amount
elif type=="W" or type=="w":
net_amount -= amount
else:
pass

#input choice
str = raw_input ("want to continue (Y for yes) : ")
if not (str[0] =="Y" or str[0] =="y") :
# break the loop
break

# print the net amount
print "Net amount: ", net_amount
```

Output

```    Enter transaction: D 10000
want to continue (Y for yes) : Y
Enter transaction: W 5000
want to continue (Y for yes) : Y
Enter transaction: D 2000
want to continue (Y for yes) : Y
Enter transaction: W 100
want to continue (Y for yes) : N
Net amount:  6900
```

ADVERTISEMENT

Preparation

ADVERTISEMENT

Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.