Python Forum
NameError: name 'self' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'self' is not defined
#9
Hi Alex,
finally i see the botton Smile . 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
Larz60+ likes this post
Reply


Messages In This Thread
NameError: name 'self' is not defined - by walter - May-18-2022, 02:31 PM
RE: NameError: name 'self' is not defined - by walter - May-19-2022, 08:42 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020