Here, we are actualizing a Python program that will be utilized to figure the Net measure of a ledger dependent on the exchanges.
Given a couple of exchanges (store, withdrawal), and we need to figure the Net Amount of that financial balance dependent on these exchanges 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
Rationale:
- For this program, we will include exchanges (till client’s decision is “Y”) with the type (“D” for the store and “W” for withdrawal) and the sum – for that we will PC it in a limitless circle while True:
- Exchanges will resemble “D 2000” that implies Deposit 2000, so there are two qualities should be removed 1) “D” is the sort of exchange and 2) 2000 is the sum to be kept.
- Exchange information will string type-convert/split the qualities to the rundown which is delimited by the space. For this, we use a string.split() strategy.
- Presently, the qualities will be in the rundown, the main worth is the sort – which is in string group and the subsequent worth is the sum – which is in additionally string arrangement, we have to change over the sum in whole number organization. So we will change over its sort (model code: sum = int(list[1])).
- From that point forward, in view of the exchange type check the conditions and including/subtracting the sum from the net sum.
- From that point forward, request the client for the following exchange and check the climate, if the client’s decision isn’t “Y” or “y”, break the circle and print the net sum.
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