Keep getting an error

I am attempting to make the Iowa Gambling Task. On the feedback routine, i have 4 text components relating to the 4 cards on the experiment and each of them have this code: $eval(‘deck_’ + resp.). I also have a code with it but when i try to run it. I get an error: ‘TypeError: string indices must be integers’:

The code is as follows:

data.importConditions('Decks_test.xlsx')
test = ('Decks_test.xlsx')

choice_a = test ['Deck_A']
choice_b = test ['Deck_B']
choice_c = test ['Deck_C']
choice_d = test ['Deck_D']

if resp_a.keys =='a':
    print(choice_a)

if resp_b.keys =='b':
    print(choice_b)

if resp_c.keys =='c':
    print(choice_c)

if resp_d.keys == 'd':
    print(choice_d)

My knowledge on python and coding is very basic so any help would be appreciated. I have also attached the experiment If you would like to take a closer look alongside the excel spreadsheet
Decks_test.xlsx (16.5 KB)
IGT.psyexp (52.2 KB)

Where did you get this code from?

data.importConditions('Decks_test.xlsx')
test = ('Decks_test.xlsx')

choice_a = test ['Deck_A']
choice_b = test ['Deck_B']
choice_c = test ['Deck_C']
choice_d = test ['Deck_D']

You have a loop pointing at the spreadsheet.

Also, this code is in Begin Experiment, which is before the response has been made.

Also, the rest of yoru code doesn’t refer to the names of your keyboard components.

Hi, I was working with my sister as she does coding as well and we changed it to this:
Before experiment:
import pandas as pd
import random

Begin experiment:

df = pd.read_excel(‘Decks_test.xlsx’)

random_indices = random.sample(range(len(df)), 4)

choice_a = df.iloc[random_indices[0], 0]
choice_b = df.iloc[random_indices[1], 0]
choice_c = df.iloc[random_indices[2], 0]
choice_d = df.iloc[random_indices[3], 0]

Each frame:
for key in event.getKeys():
if key in [‘a’, ‘b’, ‘c’, ‘d’]:
if key == ‘a’:
print(choice_a)
elif key == ‘b’:
print(choice_b)
elif key == ‘c’:
print(choice_c)
elif key == ‘d’:
print(choice_d)

I don’t really understand the tabs in detail on the code properties and when i run the experiment and press a key, the numbers appear all at once. How do i change the code so just one number appears

While you can use pandas, there doesn’t seem to be an advantage for you in doing so in this case above using a PsychoPy Builder loop.

While you can use event.getKeys, there doesn’t seem to be an advantage for you in doing so in this case above using a PsychoPy keyboard component.

I also wouldn’t use $eval('deck_A' + resp.a) in a text component.

The text component for each choice for example Deck A is just: $(choice_a). i removed event.getkeys but kept pandas for now. I just don’t know why it keeps showing the numbers all at once.

The aim is for the feedback routine is that when a key is pressed, the next frame says ‘you won £100’ or if it’s negative to say ‘you lost £100’. I also don’t want the values to be in order from the excel spreadsheet, i want it to be randomised.