Discussion


 

Q2. Write the following codes and submit the files in the “Submission.” Note. You first need to write the code related to Q1(a), then write the code related to part Q1(b).  Both files need to be saved in the same place.

Submission:

1. A summary of the program, what does the program do

2. Screenshot of the Running program

Q1.

a). Note , that you must save the following code Q1(a) with the name “bankaccount.py” 

# The BankAccount class simulates a bank account.

class BankAccount:

    # The __init__ method accepts an argument for

    # the account’s balance. It is assigned to

    # the __balance attribute.

    def __init__(self, bal):

        self.__balance = bal

    # The deposit method makes a deposit into the

    # account.

    def deposit(self, amount):

        self.__balance += amount

    # The withdraw method withdraws an amount

    # from the account.

    def withdraw(self, amount):

        if self.__balance >= amount:

            self.__balance -= amount

        else:

            print(‘Error: Insufficient funds’)

    # The get_balance method returns the

    # account balance.

    def get_balance(self):

        return self.__balance

 ========================== 

b): You can save the following code with any name, but the Q1(a) must be saved as bankaccount.py because you are import this file as shown in the following code.

# This program demonstrates the BankAccount class.

import bankaccount

def main():

    # Get the starting balance.

    start_bal = float(input(‘Enter your starting balance: ‘))

    # Create a BankAccount object.

    savings = bankaccount.BankAccount(start_bal)

    # Deposit the user’s paycheck.

    pay = float(input(‘How much were you paid this week? ‘))

    print(‘I will deposit that into your account.’)

    savings.deposit(pay)

    # Display the balance.

    print(f’Your account balance is ${savings.get_balance():,.2f}.’)

    # Get the amount to withdraw.

    cash = float(input(‘How much would you like to withdraw? ‘))

    print(‘I will withdraw that from your account.’)

    savings.withdraw(cash)

    # Display the balance.

    print(f’Your account balance is ${savings.get_balance():,.2f}.’)

# Call the main function.

main()