1

(Not asking about the solution for this problem, just trying to understand the error)

I get the following error

UnboundLocalError: local variable new_list referenced before assignment

while trying to run this piece of code. My logic was "If new_list doesn't exist then create new_list, otherwise use assign new_list.next, but looks like it doesn't work in this case.

Can someone explain to me why, and what should I do to fix this error?

def addTwoNumbers(self, l1, l2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    temp = 0
    while l1 or l2:
        if l1 and l2:
            new_val = (l1.val + l2.val) % 10 + temp
            new_node = ListNode(new_val)
        if l1:
            new_node = ListNode(l1.val + temp)
        if l2:
            new_node = ListNode(l2.val + temp)
        if not new_list:
            new_list = new_node
        else:
            new_list.next = new_node
        if new_val and new_val > 9:
            temp = 1
        else:
            temp = 0
    return new_list
Perplexabot
  • 1,852
  • 3
  • 19
  • 22
Katie
  • 811
  • 1
  • 10
  • 15
  • 1
    Well just like you said: it doesn't exist. So you can't do `if not new_list`. – Daniel Roseman Jul 01 '19 at 21:10
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Your posted code defines a function and quits without executing anything. We don't do desk-checking very often. – Prune Jul 01 '19 at 21:10
  • @prune The OP's code is actually exactly all that's necessary to reproduce the `UnboundLocalError` in this case. I have a pretty thorough explanation of `UnboundLocalError` [in this answer](https://stackoverflow.com/a/18616455/982257). In the case of the OP's code they assign to `new_list` (implicitly making that a local variable) *after* checking the value of `new_list` (where it's presumed an already existing non-local variable). – Iguananaut Jul 01 '19 at 22:30
  • @Iguananaut: I tagged it because I got no such error with Python 3.6.8. However, thanks for the link; I've tagged that as a "favorite" for later dup hammers. – Prune Jul 01 '19 at 23:50

1 Answers1

1

When l1 and l2 both are falsy, while loop is not run and your function goes straight to return new_list -- at that point the name new_list is not bound to any object i.e. it is not yet assigned to an object.

The usual fix is to set new_list to a default value before the while loop e.g.:

def addTwoNumbers(self, l1, l2):
    temp = 0
    # Here
    new_list = []
    while l1 or l2:
        ...
        ...
    return new_list

you can also return early like:

def addTwoNumbers(self, l1, l2):
    if not any(l for l in {l1, l2}):
        return []

    temp = 0
    while l1 or l2:
        ...
heemayl
  • 39,294
  • 7
  • 70
  • 76