Question
PYTHON HELP The Assignment requires to follow instructions and criteria to the following Python program class BankAccount: def __init__(self, name, initial_balance): self.name = name self.balance
PYTHON HELP
The Assignment requires to follow instructions and criteria to the following Python program
class BankAccount:
def __init__(self, name, initial_balance):
self.name = name
self.balance = initial_balance
def deposit(self, amount):
self.balance = self.balance + amount
def withdraw(self, amount):
# b) Raise an exception as appropriate
self.balance = self.balance - amount
def print_balances(account_a, account_b):
print('== Account balances ==')
print(f'{account_a.name:>9s}: ${account_a.balance:7.2f}')
print(f'{account_b.name:>9s}: ${account_b.balance:7.2f}')
total_balance = account_a.balance + account_b.balance
print(f' TOTAL: ${total_balance:7.2f}')
# a) Write your transfer_funds function here
account_a = BankAccount('Savings', 1000)
account_b = BankAccount('Debit', 200)
print_balances(account_a, account_b)
another_transfer = 'y'
while another_transfer == 'y':
# c) Handle input error
amount = float(input('Enter transfer amount ($): '))
# d) Print an appropriate message if an exception is encountered
transfer_funds(amount, account_a, account_b)
print_balances(account_a, account_b)
another_transfer = input('Perform another transfer? (y): ')
For this task, you are to write code that handles the transfer of funds between two bank accounts. Instructions As this task relates to tranferring funds between bank accounts, a BankAccount class has been provided, which supports depositing and withdrawing funds. A simple interactive loop has already been implemented, which allows the user to repeatedly transfer funds from one account to another. a) Defining the function You are to write a function called transfer_funds with three parameters: the amount to transfer, the account the funds are coming from, and the account the funds are going to. It should use the appropriate instance methods on each of the accounts to update their balances as required. b) Preventing negative balances If you tested your solution thoroughly in the previous part, you probably came across a logic error in the program--there's nothing to stop us from transferring funds from an account, even if the account balance becomes negative! You are to fix this problem by raising an exception and preventing the account balance from becoming negative. You should do this by raising a in the appropriate place in the class. _Hint: The order in which the withdrawal and deposit occur in matters-_the wrong order will result in the creation of free money!_ c) Exception handling At this point, the program prevents a transfer occurring if there aren't enough funds in the "from" account. However, simply crashing a program isn't a very nice user experience. You are to modify your program so that it handles the ValueError and displays ***Transfer cancelled*** to the user. The program should otherwise continue as normal, with the user being asked whether they would like to perform another transaction. A failed transaction should not result in either account balance changing. Hint: A little rusty at exception handling? Refer back to Topic 8 and the associated lab for examples. Criteria During marking we will check that your program: - Defines a function with correct parameters and name for transfer_funds . (2 marks) - Calls the appropriate methods to deposit and withdraw funds. Does not directly access the instance variable from within marks) - Must not accept amountStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started