为什么我的 if-elif-else 语句都输出相同的值?

Why is my if-elif-else statement all outputting the same value?

提问人:Annie Cooper 提问时间:10/23/2023 最后编辑:Annie Cooper 更新时间:10/23/2023 访问量:75

问:

示例 DataFrame 我正在尝试评估文件的 、 和 列中值的各种条件。根据这些条件,我想将每个主题列出的所有单词的值设置为 1(正确)或 -1(不正确)。testResp.keys_rawlocationmoney.csvword_value_dict[word]

但是,每次我打印值的输出时,它们都打印为 -1,而情况并非如此。我正在寻求解决此问题的帮助。

    # get correct/incorrect for each word from testTrials.csv
    word_value_dict = {}
    try:
        df = pd.read_csv(s + 'testTrials.csv')
        with open(s + 'testTrials.csv') as testTrials_file:
            for row_index, row in df.iterrows():
                if pd.isna( row['money'] ) or row['money'] == 'money':
                    continue
                word = row['word']
                money = float( row['money'] )
                ISI = float( row['ISI'] )
                response = row['testResp.keys_raw']
                location = float( row['location'])
                print(f"{s} : Row {row_index}: Response={response}, Location={location}, Money={money}")

                # check if the money value corresponds to a correct word
                if response == 'c' and location == -150 and money == 1:
                    word_value_dict[word] = 1 #correct word
                elif response == 'a' and location == 150 and money == 1:
                    word_value_dict[word] = 1 #correct word
                elif response == 'c' and location == -150 and money == 0.01:
                    word_value_dict[word] = 1 #correct word 
                elif response == 'a' and location == 150 and money == 0.01:
                    word_value_dict[word] = 1 #correct word
                elif response == 'c' and location == 0:
                    word_value_dict[word] = -1 #incorrect word
                elif response == 'a' and location == 0:
                    word_value_dict[word] = -1 #incorrect word
                elif response == 'b' and location == 0:
                    word_value_dict[word] = 1 #correct word
                else:
                    word_value_dict[word] = -1 #invalid value
    except FileNotFoundError:
        print(f'File not found for subject: {s} - testTrials.csv')
        continue 

                if word in word_value_dict.keys():
                    correct = word_value_dict[word]
                else:
                    correct = 0

                print({correct}, file=fout, end='')
    except FileNotFoundError:
        print(f'File not found for subject: {s} - studyTrials.csv')

我以前尝试过:

                if response == 'c' and location == -150 and row['money'] == 1:
                    word_value_dict[word] = 1 #correct word
                elif response == 'a' and location == 150 and row['money'] == 1:
                    word_value_dict[word] = 1 #correct word
                elif response == 'c' and location == -150 and row['money'] == 0.01:
                    word_value_dict[word] = 1 #correct word 
                elif response == 'a' and location == 150 and row['money'] == 0.01:
                    word_value_dict[word] = 1 #correct word
                elif response == 'c' and location == 0:
                    word_value_dict[word] = -1 #incorrect word
                elif response == 'a' and location == 0:
                    word_value_dict[word] = -1 #incorrect word
                elif response == 'b' and location == 0:
                    word_value_dict[word] = 1 #correct word
                else:
                    word_value_dict[word] = -1 #invalid value

#this just outputted all the values as -1 as well. 

我也试过:

 else:
                    word_value_dict[word] = None #invalid value


#This outputted all the values as None
python-3.x pandas 字典 if-statement opencsv

评论

1赞 Jacob H 10/23/2023
如果不看到实际的数据帧,就很难说。你能发布一个得到这个结果的示例 df 吗?也许由于大写或空格或其他原因,您的响应值不匹配。还有什么意义呢?我在任何地方都没有看到引用,无论如何您已经加载了它。with open(s + 'testTrials.csv') as testTrials_filetestTrials_filepd.read_csv
0赞 Annie Cooper 10/23/2023
@JacobH我刚刚上传了一个示例数据框,但是我确实不希望照片中出现每个按键输入之前都有一个空格,但我只是为这个示例添加了额外的空间,因为单元格从“a”自动更正为“a”。至于testTrials_file的开放,我上周重写了很多剧本,忘了把那部分拿出来,很好!
0赞 Jacob H 10/23/2023
如果您的响应值在 excel 中用引号括起来,那么它们在 python 中也会用引号括起来,就像而不仅仅是 .您可以在将它们拉入 python 后使用 ."'a'""a"print(response)
0赞 moken 10/23/2023
请小心您的代码示例。重复异常:第一个异常包含无效的语句,并且该条件的缩进不正确。但是,根据您的屏幕截图,列名称为“testResp.key_raw”。代码中有一些未定义的变量。except FileNotFoundError:continueif word in word_value_dict.keys():row['testResp.keys_raw']

答:

0赞 moken 10/23/2023 #1

如前所述,例如,在第一个循环中,您的值将是

{s} : Row 0: Response='a', Location=150.0, Money=1.0

因此,“response”等于 “'a'” (“\'a\'”),因此与“a”相比将返回 FALSE。

在获取单元格值时,可以使用“replace”删除值两边的单引号。

response = row['testResp.key_raw']
# change to 
response = row['testResp.key_raw'].replace("'","")


更改条件检查以包含单引号

elif response == 'a' and location == 150 and money == 1:
# change to
elif response == "\'a\'" and location == 150 and money == 1: