python名称错误:class";银行帐户“;没有定义
我有一个python代码,收到以下错误:python名称错误:class";银行帐户“;没有定义,python,Python,我有一个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 =Ba
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:
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)
第四您使用的功能名称样式不正确
displayball
应命名为display\u balance
请参阅本手册中的官方命名约定
一点建议:您似乎正在编辑器中编写代码。
也许您应该研究一些IDE,比如pycharm社区版。
这些工具可以帮助您处理诸如打字错误和缩进之类的小事故。我在Windows8下使用了python2.7,tks!您是否正确复制和粘贴缩进?看起来不对劲。您这里还有一个输入错误:
self.banlance=self.balance+amount
请将nyAccount.draw(57.55)更改为MyAccount.draw(57.55)。。它没有给出任何错误。根据回溯myAccount=BankAccount(00000,“XXXXXX”)
是类BankAccount
定义的一部分,因此给出了错误。你应该适当地缩进你的程序
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."