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

问题描述 投票:0回答:1

example dataframe 我正在尝试评估

testResp.keys_raw
文件的
location
money
.csv
列中的值的各种条件。根据这些条件,我想将每个主题列出的所有单词的
word_value_dict[word]
中的值设置为 1(正确)或 -1(不正确)。

但是,每次我打印值的输出时,它们都打印为 -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 dictionary if-statement opencsv
1个回答
0
投票

正如前面提到的,例如在第一个循环中,您的值将是

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

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

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

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:
© www.soinside.com 2019 - 2024. All rights reserved.