简体   繁体   English

Python名称错误:类“ BankAccount”未定义

[英]python Name error: class“BankAccount” is not defined

I have a python code and I receive following error: 我有一个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

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! 非常感谢您的投入!

There are several problems with your code 您的代码有几个问题

First of all, your indentation is off, please see this PEP to learn the correct indentation. 首先 ,您的缩进已关闭,请参阅此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."

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 displayBalance应命名为display_balance

Please see the official naming conventions in this PEP 请参阅此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. 也许您应该研究一些IDE,例如pycharm社区版。
Those help you with happy little accidents like typos and indentation. 这些可以帮助您解决错字和缩进等快乐的小事故。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM