计算器:Python class 中的名称错误
Calculator: Name Error in Python class
我正在构建一个计算器。我得到了我的代码的 "NameError: name 'self' is not defined the statement "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())
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())
这是因为你有缩进错误:
这就是计入 numpress
的所有内容:
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):
。
要解决此问题,您只需在“outside”
行中添加 4 个空格
我正在构建一个计算器。我得到了我的代码的 "NameError: name 'self' is not defined the statement "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())
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())
这是因为你有缩进错误:
这就是计入 numpress
的所有内容:
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):
。
要解决此问题,您只需在“outside”
行中添加 4 个空格