vendredi 22 octobre 2021

Python function stops execution after if-statement, when if statement is True [closed]

I have a weird problem, which I find very confusing. I am trying to run a function, which is a bit long and has multiple if statements. However, after the second to last if-statement, the code stops executing the rest of the function when this if-statement is True. Conceptually my code looks something like this:

def myFunc(create_csv):
    do some stuff (create a pandas dataframe, named df and a list, named list)

    if len(list) == 13:
        df2 = pd.DataFrame([list], columns = other_list) #same columns as df
        df = df.append(df2, ignore_index=True)

    print('test')
    if create_csv == True:
        print('test2')
        df.to_csv(OUTPUT_PATH + basic_emotion + 'similar_words_to_'+ emotion + '_2.csv', sep=';', encoding='utf-8')
    else:
        print(df)

So what is happening, the second if-statement (as well as anything else following the first if-statement) in this example only executes when the first if-statement is False. I've tested this with print statements, and anything after the first if-statement is only printed when the statement is False. So none of these test print statements would print, if the first if statement is True, but the do, if it is false. I've also tried removing the first if-statement and then the function executes fully. Everything before this if-statement works fine. Could anyone explain what might cause this? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire