Why am I getting a "Name error" for a variable that was defined beforehand? [closed]
Not sure why I am getting a name error
for variable c15k
when c15k
is defined beforehand. I have tried putting the entire code in a "try except" statement without success. The goal is to run the last few functions at the end of the code on a dataframe
column that will change in size based on my filtering. Hence the c15k, c30k...
in the try
statement to cover multiple scenarios. Any help is appreciated.
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
try:
outputs = chunks(df['Delay Comment_clean2'] ,15000)
c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
except:
pass
def my_tokenizer(text):
return text.split() if text != None else []
fin = []
def combine (groups):
for i in groups:
tokens = i.map(my_tokenizer).sum()
for j in tokens:
fin.append(j)
return fin
from collections import Counter
counter = Counter(combine(c15k))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_13824/4101072889.py in <module>
1 from collections import Counter
----> 2 counter = Counter(combine(c15k))
3 # counter.most_common(10)
NameError: name 'c15k' is not defined
Since you have put your assignment in a try/except
block you will not be notified when your assignment of c15k (and other variables) fail.
You could add something as simple as a print()
in the except
body to alert you of anything going wrong in the try
body.
try:
outputs = chunks(df['Delay Comment_clean2'] ,15000)
c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
except:
print("Assignment failed")
Edit:
As is seen in the below comment it should be noted that this is generally not a good way to use try/except
. Since you are using variables defined in the try
-block you will get errors pushed forward. Obfuscating the real problem being variable assignment in the try
-block.
In your case getting the error thrown at the point of variable assignment would have been much more efficient.
try/except
is best used when you want to handle errors in a special way, or if there are elements of your code you can accept not to run but the rest can continue as usual. (In your case if chunks(df['Delay Comment_clean2'] ,15000)
does not run or if c15k
is not assigned the rest of your script does not fulfill any purpose.)