Q:

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

belongs to collection: Python basic programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Count total number of bits in a number... >>
<< Python | Program to print Palindrome numbers from ...