0
class Solution(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
    list1 = [1,2,3]
    list2 = [1,4,6]
    def mergeTwoLists(self, list1, list2):
        if not list1:
            return list2
        if not list2:
            return list1
        if list1.val < list2.val:
            list1.next = self.mergeTwoLists(list1.next, list2)
            return list1
        else:
            list2.next = self.mergeTwoLists(list1, list2.next)
            return list2

    mergeTwoLists(self, list1, list2)

Have this code above. I thought i initialized the 'self' variable in my init function... but when I try to run this program I am receiving the following error:

name 'self' is not defined

Am I missing something here? Thanks in advance.

John Wick
  • 703
  • 10
  • 22
  • 1
    You really must provide a [mcve] with proper indentation, but `mergeTwoLists(self, list1, list2)` in the class bloc (doesn't make sense to call it there) will very clearly not have `self` defined, `self` is a *paramter* to those methods. A local variable – juanpa.arrivillaga Mar 08 '22 at 17:46
  • `__init__` is supposed to be a constructor (it's called automatically with `self` set to the new instance), but you haven't defined a class here. – Samwise Mar 08 '22 at 17:46
  • 1
    `self` isn't defined; it's a parameter of the two functions, not a name that exists at whatever scope you are calling `mergeTwoLists` from. (Also, where's your `class` statement?) – chepner Mar 08 '22 at 17:46
  • So, suppose I have just a regular python file with `def foo(x, y): return x + y` in it. On the next line, I write `foo(x,y)`, then it will throw a `NameError` because `x` is not defined, obviously! – juanpa.arrivillaga Mar 08 '22 at 17:48
  • If you're trying to define a linked list class, it doesn't make sense for `mergeTwoLists` to be an instance method on that class anyway. I suggest backing up a few steps and doing an intro lesson on Python classes before diving into using them to implement your own data structures. – Samwise Mar 08 '22 at 17:50
  • sorry everyone - the first line of my code got lost after the ``` – John Wick Mar 08 '22 at 17:51
  • 1
    OK, so fundamentally, it seems like you don't understand class definitions. I suggest simply *not using them*. Of course, I suspect that this is from some online code submission thing, which idiotically forces you to create a `Solution` class – juanpa.arrivillaga Mar 08 '22 at 17:54
  • yes @juanpa.arrivillaga :( you are spot on. I was trying to understand this problem, and it was hard to do so without seeing the code actually working/outputting anything. – John Wick Mar 08 '22 at 17:55
  • 1
    OK, so if you want to play around with your code locally outside of this submission thing (which runs this automatically), move the lines where you define those lists **outside the class definition**, then do `Solution().mergeTwoLists(list1, list2)` – juanpa.arrivillaga Mar 08 '22 at 17:58
  • thank you so much @juanpa.arrivillaga. For some reason I'm getting a new error: 'list' object has no attribute 'val'. – John Wick Mar 08 '22 at 19:15
  • Yeah because you ar doing `if list1.val < list2.val:` but list objects don't have a `val` attribute. Not sure what you expected that to do. – juanpa.arrivillaga Mar 08 '22 at 19:24
  • i'm dumb , I probably need to go back to the basics... sorry about that @juanpa.arrivillaga – John Wick Mar 08 '22 at 19:58
  • these l33tcode questions ... they accept solutions that dont seem to actually compile and run correctly – John Wick Mar 08 '22 at 19:58
  • 1
    @JohnWick almost certainly, looking at this code, these are supposed to be some custom linked-list objects, not the built-in `list` objects. – juanpa.arrivillaga Mar 08 '22 at 20:40
  • thanks @juanpa.arrivillaga - so i'm guessing i will just have to completely rewrite/refactor my code (for this to be 'correct') correct? – John Wick Mar 09 '22 at 15:23

0 Answers0