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

2024-10-03 15:26:35 发布

您现在位置: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 =BankAccount(00000, "XXXXXX")
NameError: name 'BankAccount' is not defined

代码:

^{pr2}$

在创建类的实例之前,我已经定义了该类。
为什么找不到我的班?我真的很感谢你的意见!在


Tags: 代码inpymost错误linecallusers
1条回答
网友
1楼 · 发布于 2024-10-03 15:26:35

你的代码有几个问题

首先,首先,你的缩进是关闭的,请看这个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."

秒:
您在存款

^{pr2}$

应该是

self.balance = self.balance + amount

第三
你有个打字错误

nyAccount.withdraw(57.55)

应该是

myAccount.withdraw(57.55)

第四
您使用的函数名称样式不正确。在

displayBalance应命名为display_balance

请参阅此PEP中的正式命名约定

一点建议:
似乎您正在编辑器中编写代码。
也许你应该看看像pycharm社区版这样的ide。
这些可以帮助你解决一些小问题,比如打字错误和缩进。在

相关问题 更多 >