我有一个python代码,我收到以下错误:
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
代码:
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)
我在创建类的实例之前已经定义了这个类 为什么找不到我的班级?我真的很感激你的意见!
答案 0 :(得分:2)
您的代码有几个问题
首先,您的缩进已关闭,请参阅此PEP以了解正确的缩进。
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."
<强>第二强>
您有存款
self.banlance = self.balance + amount
应该是
self.balance = self.balance + amount
<强>第三强>
nyAccount.withdraw(57.55)
应该是
myAccount.withdraw(57.55)
<强>四强>
你没有使用正确的功能名称风格。
displayBalance
应命名为display_balance
请参阅此PEP
中的官方命名惯例 一点建议:
好像你是在编辑器中编写代码
也许你应该研究像pycharm社区版这样的IDE
这些可以帮助你处理像拼写错误和缩进这样的快乐小事。