Reputation: 9
I have 4 text stimuli which I want to randomise their locations. I did this at the beginning of routine
Posi=[’[4.95,0]’,’[-4.95,0]’,’[0,4.95]’,’[0,-4.95]’]
shuffle(Posi)
Then, turning to the builder, I typed
$Posi[0], $Posi[1]
in the ‘position’ column and so on, for the 4 stmuli. I also set that to ‘set every repeat’
But I keep getting this
ValueError: could not convert string to float: [-4.95,0]
I don’t understand how I should change the input, because there is no problem if I just plainly put [x,y] into position.
Thanks!
Upvotes: 0
Views: 231
Reputation: 343
Like brittUWaterloo already stated, you are currently effectively creating a list of strings, not a list of lists (of coordinates), as you intended:
>>> pos = ['[4.95, 0]', '[-4.95, 0]', '[0, 4.95]', '[0, -4.95]']
>>> pos[0]
'[4.95, 0]'
>>> type(pos[0])
<class 'str'>
Note that I also changed the variable name and inserted spaces to produce more readable code that follows common coding style guidelines.
So, the first thing you need to do is remove the quotation marks to stop producing strings:
>>> pos = [[4.95, 0], [-4.95, 0], [0, 4.95], [0, -4.95]]
>>> pos[0]
[4.95, 0]
>>> type(pos[0])
<class 'list'>
Then, turning to the builder, I typed
$Posi[0], $Posi[1]
What you are trying to achieve here is, I believe, using the x, y coordinates of the very first element of the (shuffled) list of possible coordinates. I believe the current syntax is not fully correct; but let's have a closer look what would potentially happen if it were:
>>> pos[0], pos[1]
([4.95, 0], [-4.95, 0])
This would produce two coordinate pairs (the first two of the shuffled list). That's not what you want. You want the x and y coordinates of the first list pair only. To get the first coordinate pair only, you would to (in "pure" Python):
>>> pos[0]
[4.95, 0]
Or, in the Builder, you would enter
$pos[0]
into the respective coordinates field.
So to sum this up, in your Code component you need to do:
pos = [[4.95, 0], [-4.95, 0], [0, 4.95], [0, -4.95]]
shuffle(pos)
And as coordinate of the Text components, you can then use
$pos[0]
Upvotes: 0
Reputation: 1473
When you use those single quotes you are telling python that you are creating a string, that is a list of characters - not a number. Programs have types which say what a value is. '0.44' is a string of characters not a number.
>>> pos = [[0.2,0.0],[0.1,1]]
>>> pos[0]
[0.2, 0.0]
>>> pos[0][0]
0.2
>>> pos[0][0]+ 3.3
3.5
Upvotes: 1