不理解此“ NameError:名称'self'未定义”错误

时间:2018-10-16 19:10:49

标签: python python-3.x

我一直收到相同的错误消息:

  

跟踪(最近一次通话最近):文件“”,第1行,在
  my_wallet.checkBalance(self)NameError:未定义名称'self'

balance = 0
def intro():
    global balance
    balance = int(input("How much would you like to start with? No more than 200£! ")
    if balance > 200:
        print("I said less than 200£!")
        intro()
class wallet:
    def __init__(self,money,broke):
        self.broke = False
        self.money = balance
    def checkBalance(self):
        self.money = balance
        print(balance)
    def noCash(self):
        if balance <= 0:
            print("You're done! No more £££!"
        self.broke = True

1 个答案:

答案 0 :(得分:1)

从异常情况来看,您似乎试图通过传递my_wallet.checkBalance作为参数来调用self

尝试执行my_wallet.checkBalance()而不是my_wallet.checkBalance(self)

self参数自动传递给python中的类方法