In this article, we are going to discuss a class 11 python dictionary program that accepts transaction details in dictionary form and displays the balance accordingly. Let us begin!
Topics Covered
Class 11 Python Dictionary Program – Bank transaction and update balance
The complete definition of a program is as follows:
Write a program to accept transaction details in dictionary form and display updated balance.
Let us begin the process for the program.
Variables Required for Input and Output
So as per the questions given, we need the following variables:
- Balance
- A dictionary accepts the transaction details where transaction type (D for Debit and C for Credit) as a key and amount as a value
- i for traversing a dictionary in for loop
Steps
- Declare a variable for initial balance. In our program its bal = 50000
- Print the initial balance
- Prompt for transaction detail using eval(inut())
- Take a for loop to traverse a dictionary
- Now apply if condition for the appropriate function as per the transaction
- Check the key, if it is ‘D’ deduct value from bal
- If the key is ‘C’ add the value into bal
- Print the updated closing balance
Code
Observe the code for the program:
'''Write a program to accept transaction details
in dictionary form and display balance.'''
#For example:
#Enter transaction:{'D':5000}
#Output should be:
'''Initial Balance - 50000
Closing Balance after transaction - 45000'''
#Step 1 Initiate Balance
bal=50000
#Input transaction details
tr=eval(input("Enter transaction detail:"))
#Printing Initial Balance
print("Initial Balance - ",bal)
#Traversing Dictionary to perform the transaction
for i in tr:
#Checking if transaction for debit and update balance
if i=='D':
#Update balance
bal-=tr[i]
#Checking if transaction for credit and update balance
if i=='C':
bal+=tr[i]
#Printing closing balance
print("Closing Balance After transaction - ",bal)
Download Code
Follow this link to download class 11 python dictionary program bank account transaction.
Output
Watch this video for practical demo:
I hope this program helps you to understand the concept dictionary and how to work with a dictionary in python. If you have any doubts or queries or want to share your views about our website you are welcome to share.
Your views/suggestions/feedbacks related to this article class 11 python dictionary program are also welcome. Feel free to share in the comment section.
Thank you for reading this article.
For more programs for class 11, follow this link: