NameError: name 'self' is not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: NameError: name 'self' is not defined (/thread-37252.html) Pages:
1
2
|
NameError: name 'self' is not defined - walter - May-18-2022 Hi guys, i am a newbie in Python. I have a problem with an OOP issue. This is my code class Solution: def __init__(self): self.head = None def addTwoNumbers(self, lista1, lista2): carry = 0 Current1 = lista1.head Current2 = lista2.head ResultCurrent = self.head while (Current1.next != None and Current2.next != None ): if (Current1.data)+(Current2.data) > 10: datanew = Current1.data + Current2.data - 10 + carry carry = 1 ResultCurrent.insert(datanew) else: datanew = Current1.data + Current2.data + carry ResultCurrent.insert(datanew) carry = 0 Current1 = Current1.next Current2 = Current2.next ResultCurrent = ResultCurrent.nextFollow the main: def main(): LL1 = LinkedList() LL1.insert(3) LL1.insert(4) LL1.insert(5) LL1.printLL() LL2 = LinkedList() LL2.insert(6) LL2.insert(7) LL2.insert(8) LL2.printLL() result = Solution() result.addTwoNumbers(self, LL1, LL2) result.printListLinked()When i try to run this code i have an errore regarding the first argument of method of Solution class addTwoNumbers. Thanks to community for your help. Walter RE: NameError: name 'self' is not defined - walter - May-18-2022 (May-18-2022, 02:31 PM)walter Wrote: Hi guys, RE: NameError: name 'self' is not defined - walter - May-18-2022 (May-18-2022, 02:31 PM)walter Wrote: Hi guys, RE: NameError: name 'self' is not defined - Axel_Erfurt - May-18-2022 Please use the button insert python to insert your code. RE: NameError: name 'self' is not defined - walter - May-19-2022 Hi Axel, i have upload the entire code. I try to search in the web the explanation but i did't understand. I don't quite understand the use of the self in un a class method. I couldn't even solve this simple code. Walter RE: NameError: name 'self' is not defined - Axel_Erfurt - May-19-2022 I mean the insert python here. RE: NameError: name 'self' is not defined - walter - May-19-2022 Hi Axel, i am so sorry but i don't see any button on my browser to insert my python code. Walter RE: NameError: name 'self' is not defined - Axel_Erfurt - May-19-2022 Did you not see the python button in the reply window? RE: NameError: name 'self' is not defined - walter - May-19-2022 Hi Alex, finally i see the botton . So finally i can upload my code. # A single node of a singly linked list class Node: # constructor def __init__(self, data = None, next=None): self.data = data self.next = next # A Linked List class with a single head node class LinkedList: def __init__(self): self.head = None # insertion method for the linked list def insert(self, data): newNode = Node(data) if(self.head): current = self.head while(current.next): current = current.next current.next = newNode else: self.head = newNode # print method for the linked list def printLL(self): current = self.head while(current): print(current.data) current = current.next class Solution: def __init__(self): self.head = None # insertion method for the linked list def insert(self, data): newNode = Node(data) if(self.head): current = self.head while(current.next): current = current.next current.next = newNode else: self.head = newNode # print method for the linked list def printLinkedList(self): current = self.head while(current): print(current.data) current = current.next def addTwoNumbers(self, lista1, lista2): carry = 0 Current1 = lista1.head Current2 = lista2.head ResultCurrent = self.head while (Current1.next != None and Current2.next != None ): if (Current1.data)+(Current2.data) > 10: datanew = Current1.data + Current2.data - 10 + carry carry = 1 ResultCurrent.insert(datanew) else: datanew = Current1.data + Current2.data + carry ResultCurrent.insert(datanew) carry = 0 Current1 = Current1.next Current2 = Current2.next ResultCurrent = ResultCurrent.next def main(): LL1 = LinkedList() LL1.insert(3) LL1.insert(4) LL1.insert(5) LL1.printLL() LL2 = LinkedList() LL2.insert(6) LL2.insert(7) LL2.insert(8) LL2.printLL() result = Solution() result.addTwoNumbers(self, LL1, LL2) result.printLinkedList() if __name__ == "__main__": main()The follow error File "c:\PYTHON\000-WEB SITE LEETCODE\SommaDidueNumeri\SommaDueNumeri.py", line 89, in <module> main() File "c:\PYTHON\000-WEB SITE LEETCODE\SommaDidueNumeri\SommaDueNumeri.py", line 85, in main result.addTwoNumbers(self, LL1, LL2) NameError: name 'self' is not defined PS C:\Users\wrieppi> Help me to write the class solution in a better way. And explain me my error. Walter RE: NameError: name 'self' is not defined - Axel_Erfurt - May-19-2022 remove self result.addTwoNumbers(LL1, LL2) and you get
|