user12907213
user12907213

Reputation:

NameError when I try to append values

Could you please tell me why it does not find num in the code:

from collections import defaultdict
import re

result = defaultdict(list)

for l in my_list:
    k = None
    for v in l:
        if v in keywords:
            k = v
        if re.match(r'[0-9,.]+$', v):
            num = v
    if k is not None:
        result[k].append(num)

Error:

> --------------------------------------------------------------------------- NameError                                 Traceback (most recent call
> last) <ipython-input-84-31a1ed6e427e> in <module>
>      12             num = v
>      13     if k is not None:
> ---> 14         result[k].append(num)
>      15 
> 
> NameError: name 'num' is not defined

I cannot understand this error.

Upvotes: 0

Views: 231

Answers (4)

Roshin Raphel
Roshin Raphel

Reputation: 2699

In your code, the num = v is not always executed, it is run only when the if condition is True. First initialize num with a zero value, it will solve the error.

from collections import defaultdict
import re

result = defaultdict(list)
num = 0
for l in my_list:
    k = None
    for v in l:
        if v in keywords:
            k = v
        if re.match(r'[0-9,.]+$', v):
            num = v
    if k is not None:
        result[k].append(num)

Upvotes: 1

Joshua
Joshua

Reputation: 52

I have placed comments in the bellow code. the "if condition" never got true hence the variable num never initialized.

     if re.match(r'[0-9,.]+$', v): # this if condition never got true
         num = v     # the initialization never executed

also it would be a good idea to check "re.match()" documentation.\

Why the 'if condition' never got true, is because in your code you never included the '$' inside the character class.

if re.match(r'[0-9,.]+$', v): # the $ should be inside [0-9,.,$]
                num = v

'$' is usually a metacharacter, but inside a character class it’s stripped of its special nature. I tried with my own string.

import re
String = "0,1,2,3,4,5,6,7,8,9,.,$"

if re.match(r"[0-9,.,$]", String):# $ should be inside character class
    print("matched") # this print statement executes output is `matched`
else:
    print("not matched") 

The output of my code prints matched

Upvotes: 0

Red
Red

Reputation: 27567

It's simple:

from collections import defaultdict
import re

result = defaultdict(list)

for l in my_list:
    k = None
    for v in l:
        if v in keywords:
            k = v
        if re.match(r'[0-9,.]+$', v): # Here is an if statement, and num only gets defined if the condition meets it
            num = v
    if k is not None:
        result[k].append(num)

If that if statement I commented on never meets, num never gets defined.

Upvotes: 0

Jonas
Jonas

Reputation: 1769

The variable only gets created if your statment is true:

if re.match(r'[0-9,.]+$', v):
        num = v

If the statement is not true, the variable will not be created, which seems to be the case.

Upvotes: 0

Related Questions