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
#1
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.next 
Follow 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
Larz60+ write May-19-2022, 08:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
(May-18-2022, 02:31 PM)walter Wrote: 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.next

Follow 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
Larz60+ write May-19-2022, 08:18 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Many folks will not open attachments, and code in quotes is ugly.

Attached Files

.txt   PythonCode.txt (Size: 1.98 KB / Downloads: 1)
Reply
#3
(May-18-2022, 02:31 PM)walter Wrote: 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.next

Follow 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
Reply
#4
Please use the button insert python to insert your code.
Reply
#5
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
Reply
#6
I mean the insert python here.
Reply
#7
Hi Axel,
i am so sorry but i don't see any button on my browser to insert my python code.
Walter
Reply
#8
Did you not see the python button in the reply window?

[Image: python-logo.png]
Gribouillis likes this post
Reply
#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
#10
remove self

result.addTwoNumbers(LL1, LL2)

and you get

Output:
3 4 5 6 7 8
Reply


Forum Jump:

User Panel Messages

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