Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Add a Method to a Class

Extend a class by adding a method that modifies instance state.

Python practice10 minClasses & Objects FundamentalsBeginnerLast updated April 7, 2026

Problem statement

The BankAccount class has a deposit method but is missing withdraw. Implement the withdraw method that subtracts the given amount from the account balance. Then complete process_account(initial, deposits, withdrawals) to: - Create a BankAccount with the given initial balance. - Apply each deposit in the deposits list using deposit. - Apply each withdrawal in the withdrawals list using withdraw. - Return the final balance (may be negative if withdrawals exceed funds). This exercise focuses on adding instance methods and using them to change object state.

Task

Implement a missing method on a class and use it through a helper function that performs multiple operations.

Examples

One deposit, one withdrawal

Input

process_account(100, [50], [25])

Output

125

Start at 100, deposit 50 => 150, withdraw 25 => 125, return 125.

Input format

Three parameters: initial (int), deposits (list of ints), withdrawals (list of ints).

Output format

An int representing the final account balance.

Constraints

Amounts are integers. Withdrawals may exceed balance; allow negative balances.

Samples

Sample 1

Input

process_account(0, [10, 20], [5])

Output

25

0 + 10 + 20 - 5 = 25