# Creating an n-back task in builder view

**URL:** <https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000>\
**Category:** Builder\
**Tags:** n-back\
**Created:** [February 27, 2017, 5:22pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000 "2017-02-27T17:22:10Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 3, 2017, 8:32pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/21 "2017-12-03T20:32:36Z")

</div>

> [@Please surround python code with backticks](https://discourse.psychopy.org/t/please-surround-python-code-with-backticks/1775):
>
> When you include python code on this forum please put triple “backticks” like this ``` on the line before and the line after your code. If you’ve done it correctly then your code will appear in a grey box with correct indentation like this (check the preview of your post on the right hand side before posting it) and other people will be better able to work out what might be right/wrong with your code. if myVal==True: break If you’ve already posted something that didn’t follow this rule th…

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 7, 2017, 11:45pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/22 "2017-12-07T23:45:26Z")

</div>

> [@vmp](#):
>
> after that I was not able to make run my experiment.

You need to explain to us exactly what you mean by this.

---

<div class="post-metadata">

**Author:** ![vmp](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vmp](https://discourse.psychopy.org/u/vmp)\
**Post date:** [December 12, 2017, 3:04pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/23 "2017-12-12T15:04:58Z")

</div>

I dont understand how to link my code to the builder… I wrote the code in the code component in ‘each frame’

Thats’s my new code, I did some changes in it.

```auto
from random import choice, shuffle
import pandas as pd

num = 60

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
my_list = [choice(letters), choice(letters)]
probab = list(range(num - 2))
shuffle(probab)

# We want 20% of the letters to repeat the letter 2 letters back
pourc = 20
repeatnum = num * pourc // 100
for i in probab:
    ch = prev = my_list[-2]
    if i >= repeatnum:
        while ch == prev:
            ch = choice(letters)
    my_list.append(ch)

```

I added a text component and named it $my\_list

I get this error :

```auto
  text=my_list,
NameError: name 'my_list' is not defined

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 12, 2017, 7:50pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/24 "2017-12-12T19:50:29Z")

</div>

> [@vmp](#):
>
> I dont understand how to link my code to the builder… I wrote the code in the code component in ‘each frame’

As the name on the tab implies, that code will run on every frame (i.e. on every screen refresh), which is typically 60 times per second. That isn’t what you want.

Think about when you want the code to run, and put each part in the appropriate tab. e.g. imports should happen only once, so those lines belong in the “Begin Experiment” tab.

**But in your case, these libraries are already imported by Builder, so you can (and should delete them). In particular, Builder imports `choice` and `shuffle` from `numpy.random`, so you importing it from Python’s standard `random` library will cause the intended ones to be overwritten.**

I guess some other code should only run at the beginning of each trial, and so on.

---

<div class="post-metadata">

**Author:** ![vmp](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vmp](https://discourse.psychopy.org/u/vmp)\
**Post date:** [December 13, 2017, 4:59pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/25 "2017-12-13T16:59:55Z")

</div>

It’s working, but I get the complet list at the same time.

It is possible to separate it ? I want each letter to be on a different screen.

I thought that if I put $mylist in the text component, it would be like a variable and show each letter one after the other.

Many thanks

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 13, 2017, 8:44pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/26 "2017-12-13T20:44:36Z")

</div>

> [@vmp](#):
>
> I thought that if I put $mylist in the text component, it would be like a variable and show each letter one after the other.

Computers do what you tell them to do. If you ask it to display a list of items, that list will be displayed. If you want to step through the items of the list and display them one at a time, then that is quite a different thing and needs to be implemented explicitly: computers can’t guess what you want to achieve, they only take you literally.

I think you need to draw a flow chart of what you want to implement and when. The design of the flow panel (i.e. how many (possibly nested) loops) and what you need any code to do and when, will fall naturally out of that.

---

<div class="post-metadata">

**Author:** ![vmp](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vmp](https://discourse.psychopy.org/u/vmp)\
**Post date:** [December 15, 2017, 2:23pm UTC](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000/27 "2017-12-15T14:23:07Z")

</div>

Thanks Michael for your help!  
I’m sorry I don’t understand what you mean

> [@Michael](#):
>
> I think you need to draw a flow chart of what you want to implement and when. The design of the flow panel (i.e. how many (possibly nested) loops) and what you need any code to do and when, will fall naturally out of that.

I read many thing about the flow, but it all seems to be related to an csv files or an excel file with the conditions. I understand how to do that, but in this case, I want the loop to see in the list I created in the code component.

Is there a way to link both of them?

Thanks

[Previous page](https://discourse.psychopy.org/t/creating-an-n-back-task-in-builder-view/2000.md?page=1)
