如何让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/