python - 每n步执行一次while循环

标签 python python-3.x for-loop while-loop applescript

如何让for循环中,每执行7次循环后,执行某个 Action ,然后继续循环执行7次?

现在我有一个循环使用 Python + applescript 执行一些操作。 我尝试使用 while 来创建一个应在每 7 个步骤后执行一次的条件,然后使用 excel 继续循环。循环有效,一切都很好。但在 while 开始执行的那一刻,它不想从头开始循环,它想继续执行 while,因为 count 变量从 Excel 文件中获取行。 如何解决?

from subprocess import Popen, PIPE
import xlrd

def main():
    apple_script = '''
         code
       '''
    workbook = xlrd.open_workbook('test.xlsx')
    sheet = workbook.sheet_by_index(0)
    count = 1
    for cell in sheet.col(0):
        p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        stdout, stderr = p.communicate(apple_script)
        count += 1
        while count >= 7:
            app_script = '''
                 code
                '''
            p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
            stdout, stderr = p.communicate(app_script)
            break
if __name__ == "__main__":
    main()

最佳答案

count=1
while count >= 7:
   if count % 7 == 0:
      print(count)
   count = count + 1

输出:

7
14
21
28
35
.
.
.

关于python - 每n步执行一次while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441700/

相关文章:

javascript - 如何在 JavaScript 的 for 循环中创建动态变量以返回 json?

c - 在C中将文本文件读取到数组中

c - C中使用数组的for循环

python - 列表列表更改意外地反射(reflect)在子列表中

python - 我想查找列表的总计,但出现错误

python - @dataclass Python 类中拼写错误的字段

python - 循环遍历文件并同时使用 `file.readline()` 是 "ok"的做法吗?

python - Python 中高效的组子字符串搜索?

python - 如何将 Modin pandas 数据框附加到其他数据框?

python - 使用 Sounddevice 连续播放音频