Chapter 5: Functions / Lesson 30

Banking Practice Program

🎯 Project: Banking Practice Program

Let's build a banking program that practices all the function concepts you've learned! This project combines user-defined functions, return statements, default arguments, and variable scope to create a practical banking simulation.

You'll create functions for checking balance, depositing money, withdrawing money, and displaying account information. This project will help you practice organizing code with functions and managing program state.

Project Requirements

Your banking program should include:

  • Function to check account balance
  • Function to deposit money (add to balance)
  • Function to withdraw money (subtract from balance)
  • Function to display account information
  • Use a global variable or function parameters to track balance

Step-by-Step Guide

Here's how to build the banking program:

banking_steps.py
# Step 1: Initialize account balance (global variable) balance = 1000 # Starting balance # Step 2: Function to check balance def check_balance(): global balance return balance # Step 3: Function to deposit def deposit(amount): global balance balance += amount return balance # Step 4: Function to withdraw def withdraw(amount): global balance if amount <= balance: balance -= amount return balance else: return "Insufficient funds" # Step 5: Test the functions print(f"Balance: ${check_balance()}") deposit(500) print(f"Balance after deposit: ${check_balance()}")

Complete Banking Program

Here's a complete version with all banking functions:

complete_banking.py
# Banking program with functions balance = 1000 def check_balance(): global balance return balance def deposit(amount): global balance if amount > 0: balance += amount print(f"Deposited ${amount}. New balance: ${balance}") return balance else: print("Invalid deposit amount") return balance def withdraw(amount): global balance if amount > balance: print("Insufficient funds!") return balance elif amount <= 0: print("Invalid withdrawal amount") return balance else: balance -= amount print(f"Withdrew ${amount}. New balance: ${balance}") return balance def display_account(): print(f"Current Balance: ${check_balance()}") # Test the banking functions display_account() deposit(250) withdraw(100) display_account()

Enhanced Features

Add more features to make your banking program more complete:

enhanced_banking.py
# Enhanced banking with transaction history balance = 1000 transactions = [] def deposit(amount, description="Deposit"): global balance, transactions if amount > 0: balance += amount transactions.append(f"{description}: +${amount}") print(f"✓ {description}: +${amount}") return balance def withdraw(amount, description="Withdrawal"): global balance, transactions if amount <= balance and amount > 0: balance -= amount transactions.append(f"{description}: -${amount}") print(f"✓ {description}: -${amount}") return balance def show_transactions(): print("\nTransaction History:") for transaction in transactions: print(transaction) print(f"\nCurrent Balance: ${balance}") # Test enhanced features deposit(500, "Salary") withdraw(200, "Rent") show_transactions()

Tips and Best Practices

✅ Function Design Tips

• Use clear, descriptive function names

• Validate inputs (amount > 0, sufficient funds)

• Return useful values from functions

• Use global variables carefully for shared state

• Consider using default arguments for optional parameters

💡 Testing Your Program

Test your banking program with various scenarios: normal deposits/withdrawals, insufficient funds, invalid amounts, and multiple transactions. Make sure the balance updates correctly!

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...