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

标签 python loops enumeration

我的 for 循环如下所示:

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

我想要做的是在给定的迭代次数处暂停 for 循环,并等待用户按键继续。

因此,如果我提供值 5,则每 5 次迭代就会暂停循环。

如有任何问题或需要信息,请告诉我。非常感谢所有帮助。

最佳答案

您可以按照所需的迭代次数使用input内置函数:

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

假设您希望在 n 次迭代中暂停:

n = 7

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

如果您迭代具有不同类型元素的列表:

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

关于python - 每 'n' 次迭代暂停循环并等待用户按键。 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60362276/

相关文章:

python - 适用于 OSX 10.6 上 PyQt 的 Qt 设计器

python - 在 Amazon Linux 2 上安装 AWS CDK

ios - Swift 中的循环振动

Javascript提示并提醒输入一个数字,它会循环,你将输入数字以获得它的平均值

c# - 如何将字符串转换为 int 类型的枚举?

python - 仅有时在 python 中播放代码

javascript - AngularJS 在数组中循环多个数组

c# - 我应该何时以及如何使用枚举类而不是枚举?

powershell - PowerShell中字典内的字典

python - 在 ubuntu 上使用 anaconda 安装 openCV