Reputation: 37
I have a python code and I receive following error:
Traceback (most recent call last):
File "C:/Users/SUVI/Desktop/SUVIwork/BankAccount.py", line 1, in <module>
class BankAccount:
File "C:/Users/SUVI/Desktop/SUVIwork/BankAccount.py", line 26, in BankAccount
myAccount =BankAccount(00000, "XXXXXX")
NameError: name 'BankAccount' is not defined
Code:
class BankAccount: // Here I defined the class" BankAccount"
def __init__(self, acct_number, acct_name):
self.acct_number = acct_number
self.acct_name =acct_name
self.balance = 0.0
def displayBalance(self):
print " The account balance is:", self.balance
def deposit(self, amount):
self.banlance = self.balance + amount
print "You deposited", amount
print " The new balance is :", self.balance
def withdraw(self, amount):
if self.balance >= amount:
self.balance = self.balance - amount
print "You withdrew", amount
print " The new balance is:", self.balance
else:
print "You tried to withdraw", amount
print " The account balance is:", self.balance
print " Withdrawal denied. No enough funds."
myAccount =BankAccount(00000, "XXXXXX")
print "Account name :", myAccount.acct_name
print "Account number:", myAccount.acct_number
myAccount.displayBalance()
myAccount.deposit(100)
nyAccount.withdraw(57.55)
myAccount.withdraw(67.18)
I already defined the class before creating an instance of the class.
Why it can not find my class? I really appreciate your input!
Upvotes: 0
Views: 1667
Reputation: 4255
There are several problems with your code
First of all, your indentation is off, please see this PEP to learn the correct indentation.
class BankAccount:
def __init__(self, acct_number, acct_name):
self.acct_number = acct_number
self.acct_name =acct_name
self.balance = 0.0
def displayBalance(self):
print " The account balance is:", self.balance
def deposit(self, amount):
self.balance = self.balance + amount
print "You deposited", amount
print " The new balance is :", self.balance
def withdraw(self, amount):
if self.balance >= amount:
self.balance = self.balance - amount
print "You withdrew", amount
print " The new balance is:", self.balance
else:
print "You tried to withdraw", amount
print " The account balance is:", self.balance
print " Withdrawal denied. No enough funds."
Second:
You have a typo in deposit
self.banlance = self.balance + amount
should be
self.balance = self.balance + amount
Third
you have a typo at
nyAccount.withdraw(57.55)
should be
myAccount.withdraw(57.55)
Fourth
You are not using the right funtion-name-style.
displayBalance
should be named display_balance
Please see the official naming conventions in this PEP
A little recommendation:
It seems like you are writing your code in an editor.
Maybe you should look into some IDEs like pycharm community edition.
Those help you with happy little accidents like typos and indentation.
Upvotes: 2