提问人:Annie Cooper 提问时间:10/23/2023 最后编辑:Annie Cooper 更新时间:10/23/2023 访问量:75
为什么我的 if-elif-else 语句都输出相同的值?
Why is my if-elif-else statement all outputting the same value?
问:
示例 DataFrame 我正在尝试评估文件的 、 和 列中值的各种条件。根据这些条件,我想将每个主题列出的所有单词的值设置为 1(正确)或 -1(不正确)。testResp.keys_raw
location
money
.csv
word_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
答:
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:
评论
with open(s + 'testTrials.csv') as testTrials_file
testTrials_file
pd.read_csv
"'a'"
"a"
print(response)
except FileNotFoundError:
continue
if word in word_value_dict.keys():
row['testResp.keys_raw']