0

I'm brand new to Python (psychoPy) and I have this script that I want it to repeat three times:

 i = 0
    while i < 4:

    import random
    win.setMouseVisible(False)

    this_target = random.choice(first)

    if this_target == 1:
        k = 0
        location = []
        tloc = random.randint(0, 7)
        tloc = str(tloc)
        location.append(tloc)
        gap.setPos(left_gap[tloc, : ])
        squarer.setPos(ranpos[tloc, : ])
        squarer.draw()
        gap.draw()
        while k < 4:
            loc = random.randint(0, 7)
            loc = str(loc)
            if loc not in location: location.append(loc)
            else:
                continue
            squareg.setPos(ranpos[loc, : ])
            squareg.draw()
            dist_side = random.randint(1, 2)
            if dist_side == 1:
                gap.setPos(left_gap[loc, : ])
                gap.draw()
            elif dist_side==2:
                gap.setPos(right_gap[loc, : ])
                gap.draw()

            k+=1


    elif this_target == 2:
        k = 0
        location = []
        tloc = random.randint(0, 7)
        tloc = str(tloc)
        location.append(tloc)
        gap.setPos(right_gap[tloc, : ])
        squareg.setPos(ranpos[tloc, : ])
        squareg.draw()
        gap.draw()
        while k < 4:
            loc = random.randint(0, 7)
            loc = str(loc)
            if loc not in location: location.append(loc)
            else:
                continue
            squarer.setPos(ranpos[loc, : ])
            squarer.draw()
            dist_side = random.randint(1, 2)
            if dist_side == 1:
                gap.setPos(left_gap[loc, : ])
                gap.draw()
            elif dist_side==2:
                gap.setPos(right_gap[loc, : ])
                gap.draw()

            k+=1

    win.flip()
    resp = event.waitKeys(keyList = ['z', 'm', 'q'])
    fix.draw()
    win.flip()
    core.wait(2) #accio

    this_target = random.choice(first)

    if this_target == 1:
        k = 0
        location = []
        tloc = random.randint(0, 7)
        tloc = str(tloc)
        location.append(tloc)
        gap.setPos(left_gap[tloc, : ])
        squarer.setPos(ranpos[tloc, : ])
        squarer.draw()
        gap.draw()
        while k < 4:
            loc = random.randint(0, 7)
            loc = str(loc)
            if loc not in location: location.append(loc)
            else:
                continue
            squareg.setPos(ranpos[loc, : ])
            squareg.draw()
            dist_side = random.randint(1, 2)
            if dist_side == 1:
                gap.setPos(left_gap[loc, : ])
                gap.draw()
            elif dist_side==2:
                gap.setPos(right_gap[loc, : ])
                gap.draw()

            k+=1


    elif this_target == 2:
        k = 0
        location = []
        tloc = random.randint(0, 7)
        tloc = str(tloc)
        location.append(tloc)
        gap.setPos(right_gap[tloc, : ])
        squareg.setPos(ranpos[tloc, : ])
        squareg.draw()
        gap.draw()
        while k < 4:
            loc = random.randint(0, 7)
            loc = str(loc)
            if loc not in location: location.append(loc)
            else:
                continue
            squarer.setPos(ranpos[loc, : ])
            squarer.draw()
            dist_side = random.randint(1, 2)
            if dist_side == 1:
                gap.setPos(left_gap[loc, : ])
                gap.draw()
            elif dist_side==2:
                gap.setPos(right_gap[loc, : ])
                gap.draw()

            k+=1

    win.flip()
    resp = event.waitKeys(keyList = ['z', 'm', 'q'])
    i+=1
    next_trial.draw()
    win.flip()
    event.waitKeys(keyList = ['space'])

    if resp == ['q']:
        core.quit()
        win.close()

begin.draw()
win.flip()
event.waitKeys(keyList = ['space'])

Is there a script I can just add to the beginning to because it to repeat?

2
  • 1
    put you code in function, and loop 3 time. Commented Nov 10, 2014 at 21:26
  • 1
    Just wrap it up in a for loop. Commented Nov 10, 2014 at 21:26

2 Answers 2

2

why can't you enclose it all in a for loop? - probably want to change the position of the import though...

import random

def cool_function_bro():
 i = 0
 while i < 4:


 win.setMouseVisible(False)

 this_target = random.choice(first)

 if this_target == 1:
     k = 0
     location = []
     tloc = random.randint(0, 7)
     tloc = str(tloc)
     location.append(tloc)
     gap.setPos(left_gap[tloc, : ])
     squarer.setPos(ranpos[tloc, : ])
     squarer.draw()
     gap.draw()
     while k < 4:
        loc = random.randint(0, 7)
        loc = str(loc)
        if loc not in location: location.append(loc)
        else:
            continue
        squareg.setPos(ranpos[loc, : ])
        squareg.draw()
        dist_side = random.randint(1, 2)
        if dist_side == 1:
            gap.setPos(left_gap[loc, : ])
            gap.draw()
        elif dist_side==2:
            gap.setPos(right_gap[loc, : ])
            gap.draw()

for i in range(3):
  cool_function_bro()
Sign up to request clarification or add additional context in comments.

2 Comments

This worked beautifully, thank you! Also your loop function name made me laugh. :D
ha ha, not a problem bro :)
0

you can use:

1-run script 3 time from shell if you are using linux you can run:

for i in {1..3}; do python script.py; done

2-put your code in a function and run it 3 times:

def test_func():
    # put your code here

for i in range(3):
    test_func()

3- put your code under loop

for i in range(3):
    # put your code here

2 Comments

need to factor imports out of 2 and 3 and 1 will be slower due to initial overhead
FWIW, you'd need to use range(3) to loop 3 times, range(n) iterates from 0 to n-1, inclusive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.