In: Computer Science
Deposit function.
Calculate the Balance - Deposit
If the action is Deposit 'D’, use a deposit function to add funds to the account
Deposit Input
userchoice = input ("What would you like to do?\n") userchoice = 'B' deposit_amount = 200
Deposit Output
What would you like to do? How much would you like to deposit today? Deposit was $200, current balance is $700.25
#import sys #no need for this module
#account balance
account_balance = float(500.25)
#<--------functions go here-------------------->
#printbalance function
def printbalance():
print("Account balance: $%.2f" % account_balance)
#deposit function
def deposit():
#accessing account_balance variable which is outside the function
global account_balance
deposit_amount=float(input('How much would you like to deposit today?'))
account_balance+=deposit_amount
print('Deposit was $%.2f, current balance is $%.2f' % (deposit_amount,account_balance))
#withdraw function
def withdraw():
#accessing account_balance variable which is outside the function
global account_balance
withdrawal_amount=float(input('How much would you like to withdraw?'))
#withdrawing only if there is enough balance
if account_balance
print('withdrawal_amount is greater that your account balance of $%.2f' % account_balance)
else:
account_balance-=withdrawal_amount
print('Withdrawal amount was $%.2f, current balance is $%.2f' % (withdrawal_amount,account_balance))
userchoice=''
#looping until user enters Q or q
while userchoice.upper()!='Q':
userchoice = input ("What would you like to do? (D/W/B or Q)\n")
#converting to upper case to reduce comparisons
userchoice=userchoice.upper()
#performing operations based on choice
if userchoice=='D':
deposit()
elif userchoice=='W':
withdraw()
elif userchoice=='B':
printbalance()
elif userchoice=='Q':
print('Thank you for banking with us.')
else:
print('Not a valid choice')
For the above program, we need to have the following things into consideration.
Following is the code for the same.
def printbalance(): print("Account balance: $%.2f" % account_balance) # deposit function def deposit(): # accessing account_balance variable which is outside the function global account_balance deposit_amount = float(input('How much would you like to deposit today?')) account_balance += deposit_amount print('Deposit was $%.2f, current balance is $%.2f' % (deposit_amount, account_balance)) # withdraw function def withdraw(): # accessing account_balance variable which is outside the function global account_balance withdrawal_amount = float(input('How much would you like to withdraw?')) # withdrawing only if there is enough balance if account_balance < withdrawal_amount: print('withdrawal_amount is greater that your account balance of $%.2f' % account_balance) else: account_balance -= withdrawal_amount print('Withdrawal amount was $%.2f, current balance is $%.2f' % (withdrawal_amount, account_balance)) userchoice = '' account_balance = 0 # looping until user enters Q or q while userchoice.upper() != 'Q': userchoice = input("What would you like to do? (D/W/B or Q)\n") # converting to upper case to reduce comparisons userchoice = userchoice.upper() # performing operations based on choice if userchoice == 'D': deposit() elif userchoice == 'W': withdraw() elif userchoice == 'B': printbalance() elif userchoice == 'Q': print('Thank you for banking with us.') else: print('Not a valid choice')
Following is the snippet of the above code.
Following is the sample output.
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks