Classes: Bank Account project


#1



I get the error message:
File "bankaccount.py", line 3
def init(self, name):
^
IndentationError: unindent does not match any outer indentation level


The indents look right to me. I'm not sure what's happening or why.


class BankAccount(object):
    balance = 0
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "%s account, balance of $%.2f" % (self.name, self.balance)
    def show_balance(self):
        print "%s account, balance of $%.2f" % (self.name, self.balance)
    def deposit(self, amount):
        if amount <= 0:
            print "Please enter a positive amount"
            return
  	    else:
            print "You deposited $%.2f" % (self.amount)
            self.balance += amount
    self.show_balance(balance)
    def withdraw(self, amount):
        if amount >= self.balance:
            print "You do not have enough available to withdraw that much"
        else:
            print "You are withdrawing $%.2f" % (self.amount)
            self.balance -= amount
    				self.show_balance(balance)
my_account = BankAccount("Mickey")
print my_account
my_account.show_balance(balance)
my_account.deposit("Mickey", 200)
my_account.withdraw("Mickey", 1000)
print my_account


#2

Sometimes the cure is to replace all the indentation with 4 space characters per block level. Mixed spaces and tabs can cause the above error. Be sure the else lines up with if.


#3

I went through every line to make sure each indentation was 4 spaces. Also made sure to line up the if and else (I think the copy moved it, but just in case). I still get the same error, even if I refresh. I use FireFox, if that makes a difference.


#4

Please let us have a look at your revised code. Thanks.


#5

Ok here it is:

class BankAccount(object):
    balance = 0
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "%s account, balance of $%.2f" % (self.name, self.balance)
    def show_balance(self):
        print "%s account, balance of $%.2f" % (self.name, self.balance)
    def deposit(self, amount):
        if amount <= 0:
            print "Please enter a positive amount"
            return
  	else:
            print "You deposited $%.2f" % (self.amount)
            self.balance += amount
    self.show_balance(balance)
    def withdraw(self, amount):
        if amount >= self.balance:
            print "You do not have enough available to withdraw that much"
        else:
            print "You are withdrawing $%.2f" % (self.amount)
            self.balance -= amount
    				self.show_balance(balance)
my_account = BankAccount("Mickey")
print my_account
my_account.show_balance(balance)
my_account.deposit("Mickey", 200)
my_account.withdraw("Mickey", 1000)
print my_account

#6

The deposit method does not need a return statement.

This line (in deposit method) needs more indentation. The one in the withdraw method needs less indentation.


#7

Step 10 had called for the return. I took it out and fixed the indentation issues you mentioned. It still stops at line 3 and gives the same error.


#8

Please post a link to this exercise. Thanks.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.