【问题标题】:Calculator: Name Error in Python class计算器:Python 类中的名称错误
【发布时间】:2016-03-27 21:26:28
【问题描述】:

我正在构建一个计算器。我得到了“NameError:name 'self' is not defined 我的代码的语句“if self.op.Pending == True”部分。我尝试设置等于None,但这并没有摆脱错误。如何消除错误?

class calculator():
    def __init__(self):
        self.total = 0
        self.current = ""
        self.newNumber = True
        self.opPending = False
        self.op = ""
        self.eq = False

    def numberPress (self, num): 
        self.eq = False
        temp = textbox.get()
        temp2 = str(num)

    if self.newNumber:
        self.current = temp2
        self.newNumber = False
    else:
        if temp2 == '.':
            if temp2 in temp:
                return
            self.current = temp + temp2
            self.display(self.current)

    def calcTotal(self):
        self.eq = True
        self.currrent = float(self.current)

    if self.opPending == True: #ERROR
        self.doSum()
    else:
            self.total = float(textbox.get())

【问题讨论】:

    标签: python class variables calculator statements


    【解决方案1】:

    这是因为你有缩进错误:

    这是对numpress 重要的一切:

    def numberPress (self, num): 
            self.eq = False
            temp = textbox.get()
            temp2 = str(num)
    

    下一行是函数的“outside”:

     if self.newNumber:
            self.current = temp2
            self.newNumber = False
        else:
            if temp2 == '.':
                if temp2 in temp:
                    return
                self.current = temp + temp2
                self.display(self.current)
    

    同样的事情发生在def calcTotal(self):

    要解决此问题,您只需在“outside”的行中添加 4 个空格

    【讨论】:

      【解决方案2】:

      self 没有在您的各个函数之外定义,这就是为什么在每个函数中您必须调用 def func(self...):

      因此,在函数中调用它:

      class calculator():
          def __init__(self):
              self.total = 0
              self.current = ""
              self.newNumber = True
              self.opPending = False
              self.op = ""
              self.eq = False
      
          def numberPress (self, num): 
              self.eq = False
              temp = textbox.get()
              temp2 = str(num)
      
              if self.newNumber:
                  self.current = temp2
                  self.newNumber = False
              else:
                  if temp2 == '.':
                      if temp2 in temp:
                          return
                      self.current = temp + temp2
                      self.display(self.current)
      
          def calcTotal(self):
              self.eq = True
              self.currrent = float(self.current)
      
          def call(self):
              if self.opPending == True: #ERROR
                  self.doSum()
              else:
                  self.total = float(textbox.get())
      

      【讨论】:

      • 谢谢,真不敢相信我忘记了!现在,是我构建 GUI 的时候了。
      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多