Created
May 4, 2017 02:59
-
-
Save odanga94/bb695fd2c7042bd4a8b6f1267d89193e to your computer and use it in GitHub Desktop.
Code Academy python lesson: Bank Account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This program creates a Python class tat can be used to create and manipulate a personal bank account. The Bank account class: accepts deposits, allows withdrawals, shows the balance and shows the details of the account.""" | |
class BankAccount(object): | |
balance = 0 | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return "This account belongs to %s and has a balance of $%.2f" %(self.name, self.balance ) | |
def show_balance(self): | |
print("%s's balance: $%.2f") %(self.name,\ | |
self.balance) | |
def deposit(self, amount): | |
if amount <= 0: | |
print("You cannot deposit an amount less than or equal to 0") | |
return | |
else: | |
print("You have deposited $%.2f") %(amount) | |
self.balance += amount | |
self.show_balance() | |
def withdraw(self, amount): | |
if amount > self.balance: | |
print("Sorry your account balance is insufficient for this transaction") | |
return | |
else: | |
print("You are withdrawing $%.2f") %(amount) | |
self.balance -= amount | |
self.show_balance() | |
my_account = BankAccount("Odanga") | |
print(my_account) | |
my_account.show_balance() | |
my_account.deposit(2000) | |
my_account.withdraw(1000) | |
print(my_account) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment