#1 by AngelVJimenez
I’m struggling with a bit of my code for an experiment with OTree Studio. In my experiment, 8 participants interact with each other by forming pairs at random over 30 rounds. Participants have “traits” that are represented by a string with 0s (if they don’t have a trait) and 1s (if they have a trait). In each round, each target participant can decide to transmit the traits they have (their 1s) and their partner doesn’t (their 0s) to their partner in that round. If this is the case, their partner gets those traits, and their string is updated. To do this, I have created the following group function: def copy_traits(group): subsession = group.subsession players = subsession.get_players() for player in players: if player.transmitted: # Convert traits to a list of characters traits_list = list(player.traits) # Get the partner index partner_index = player.chosen_partner if partner_index is not None: partner = players[partner_index] # Convert partner's traits to a list of characters partner_traits_list = list(partner.traits) # Iterate through characters and apply the logic for j in range(len(traits_list)): if ( traits_list[j] == '1' and partner_traits_list[j] == '0' ): traits_list[j] = '1' # Convert the modified list back to a string player.traits = ''.join(traits_list) Unfortunately, it doesn’t work. It returns the following error: IndexError: string index out of range The problem is caused by this line of code: player.traits[players[player.chosen_partner].id_in_group][j] == '0' Does somebody know how I can make this work? Thank you very much. Ángel
#2 by Trontatuma (edited )
The error is quite informative: One of the elements in your index does not exist. I've had this because I forgot that Python indexes start at 0. Should you maybe use j-1 instead?