0

我是 Python (psychoPy) 的新手,我有这个脚本,我希望它重复三遍:

 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'])

有没有我可以添加到开头的脚本,因为它要重复?

4

2 回答 2

2

为什么不能将其全部包含在 for 循环中?- 可能想改变导入的位置......

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()
于 2014-11-10T21:25:58.037 回答
0

您可以使用:

如果您使用的是 linux,则从 shell 1-run script 3 time,您可以运行:

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

2-将您的代码放入函数中并运行 3 次:

def test_func():
    # put your code here

for i in range(3):
    test_func()

3-将您的代码置于循环中

for i in range(3):
    # put your code here
于 2014-11-10T21:35:19.433 回答