I'm having trouble with the following Python code:
class Methods:
def method1(n):
#method1 code
def method2(N):
#some method2 code
for number in method1(1):
#more method2 code
def main():
m = Methods
for number in m.method2(4):
#conditional code goes here
if __name__ == '__main__':
main()
When I run this code, I get
NameError: name 'method1' is not defined.
How do I resolve this error?
The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.
What Is a NameError in Python? In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.
Just add self. in front of it:
self.method1(1)
Also change your method signitures to:
def method1(self, n):
and
def method2(self, n):
Change your code like following:
class Methods:
def method1(self,n):
#method1 code
def method2(self,N):
#some method2 code
for number in self.method1(1):
#more method2 code
def main():
m = Methods()
for number in m.method2(4):
#conditional code goes here
if __name__ == '__main__':
main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With