In: Computer Science
need to add this withdrawal and balance function to the script below:
Calculate the Balance - Withdrawal
If the action is Withdrawal ‘W’, use the withdrawal function to remove funds from the account
Hint – The formatter for a float value in Python is %f. With the formatter %.2f, you format the float to have 2 decimal places. For example:
print("Account balance: $%.2f" % account_balance)
Withdrawal Information
Withdraw Input
userchoice = input ("What would you like to do?\n") userchoice = 'W' withdrawal_amount = 100
Withdraw Output
What would you like to do? How much would you like to withdraw today? Withdrawal amount was $100, current balance is $600.25
ATM Summary Feedback Available
If you would like our automated system to review your code and give you some feedback on your project before you submit for a grade, just click the Help Me! button below.
Help Me!
withdrawal_amount is greater than your account balance of account_balance
Ensure you display the withdrawal amount and the account balance with the '$' and two decimal points.Withdrawal amount was withdrawal_amount, current balance is account_balance
Need to add the Withdrawal amount to this program:
import sys # importing the sys library
# account balance
account_balance = float(500.25)
# PPrint the balance
# This is a custom function, it returns the current balance upto 2 decimal places
def printbalance():
print('Your current balance:')
return account_balance
# the function for deposit
# This is a custom function
def deposit():
# takes in input for deposit amount
deposit_amount = float(
input("How much would you like to deposit today?\n"))
balance = account_balance + deposit_amount # calculates balance
print("Deposit was $%.2f, current balance is $%.2f" %
(deposit_amount, balance)) # prints out the balance
# function for withdraw
# this is a custom function
def withdraw():
# takes in the withdraw amount
withdraw_amount = float(input("Enter amount to withdraw"))
if(withdraw_amount > account_balance): # checks whether the amount is more than balance or not
print("$%2f is greater than account balance $%2f\n" %
(withdraw_amount, account_balance)) # if yes then
else:
balance = account_balance - withdraw_amount
print("$%2f was withdrawn, current balance is $%2f" %
(withdraw_amount, balance))
# User Input goes here, use if/else conditional statement to call function based on user input
userchoice = input("What would you like to do?\n")
if (userchoice == 'D'):
# here deposit function is called
deposit()
elif userchoice == 'W':
# here withdraw function is called
withdraw()
elif userchoice == 'B':
# here printbalance function is called
balance = printbalance()
print('{:.2f}'.format(balance))
else:
# it ends the program execution
sys.exit()
There is already withdrawal and balance function to the script. There Does seem a problem with withdrawal function.
It is not printing float upto 2 decimal places. Assuming that is your problem. That is because in this printing statement of withdraw function
print("$%2f is greater than account balance $%2f\n" %
and
print("$%2f was withdrawn, current balance is $%2f" %
%2f is written instead of %.2f (there is a Dot(.) between % and 2)
that is why it is showing more that 2 decimal places. As for print balance function it's working fine although i made a little change
Here is the fixed program
And here are the desired outputs
------------------------------
Please rate if it helps :) Happy to help!