简体   繁体   English

暂停 for 循环并等待用户按键每 'n' 次迭代。 - Python

[英]Pause for loop and wait for user key press every 'n' iterations. - Python

I have for loop which looks like this:我有一个看起来像这样的循环:

for trial in trials:
    stim.text = trial[0]
    stim.color = trial[1]
    win.flip()
    isi = random.random() * 2.0
    core.wait(isi)
    stim.draw()
    event.clearEvents()
    displaytime = win.flip()
    keys = event.waitKeys(keyList=answer_keys.keys(), timeStamped=True)
    rt =  keys[0][1] - displaytime
    trialNum +=1
    if trial[0] == trial[1]:
        condition = 'congruent'
    elif trial[0] != trial[1]:
        condition = 'incongruent'
    logfile.write('{},{},{},{},{},{:.3f}\n'.format(trialNum, trial[0], trial[1], condition, keys[0], rt))

what I want to do is pause the for loop at a given number of iterations and wait for the users key press to continue.我想要做的是在给定的迭代次数中暂停 for 循环并等待用户按键继续。

So if I supplied the value 5 this would pause the loop every 5 iterations.因此,如果我提供值 5,这将每 5 次迭代暂停循环。

Anymore questions or info required let me know.任何需要的问题或信息让我知道。 All help is greatly appreciated.非常感谢所有帮助。

you can use input built-n function at your desired number of iterations:您可以在所需的迭代次数中使用input内置函数:

input("Press Enter to continue...")

let's say that you want at n iterations to make the pause:假设您希望在n次迭代中暂停:

n = 7

for i in range(1, 20):
    if i % n == 0:
        input("Press Enter to continue...")

if you iterate over a list with different kind of elements:如果您迭代具有不同类型元素的列表:

for i, e in enumerate(my_list):
    if i % n == 0:
        input("Press Enter to continue...")
    ... other code...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM