for-loop - 'Return' outside of function - TagMerge
1'Return' outside of function'Return' outside of function

'Return' outside of function

Asked 5 months ago
0
1 answers

As the error says, your return is outside a function. Return should be used within a scope of a function, in your case, on suspect_dict() function.

You could either just loop the result and print it, without returning anything, also, you don't need to use enumerate as you're not dealing with indexes:

def suspect_dict():
    dict = pd.read_csv("suspectdict.csv", squeeze=True)
    pattern = '|'.join(dict)
    result = np.where(news_df["headline"].str.contains(pattern, na=False),1, 0)

    for value in result:
        print(value)

But if you need to return the result, you could just use return result inside the function:

def suspect_dict():
    dict = pd.read_csv("suspectdict.csv", squeeze=True)
    pattern = '|'.join(dict)
    result = np.where(news_df["headline"].str.contains(pattern,na=False),1,0)
    return result

Note that python uses indentation to understand where a block of code begins and ends, so make sure that all codes that might belong to the function are well indented.

Source: link

Recent Questions on for-loop

    Programming Languages