当我尝试创建循环时出现Python IndentationError

时间:2020-02-06 06:24:34

标签: python for-loop

我有这部分Python代码,可以正常工作

   import mss
   import mss.tools
   import datetime
   import time

    with mss.mss() as sct:
    monitor = sct.monitors[1]
    timestr = time.strftime("%Y%m%d-%H%M%S")
    sct.compression_level = -1
    output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
    sct_img = sct.grab(monitor)
    mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
    print(output)

我会在每个循环之间添加一个无终止循环/循环,并在每个循环之间延迟10秒,因此我以这种方式添加了 while

    import mss
    import mss.tools
    import datetime
    import time 

        while count < 100000000:

        with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

        time.sleep(10)
        count += 1

但它返回此值

IndentationError:unindent与任何外部缩进级别都不匹配

如何在我的代码中添加循环?

3 个答案:

答案 0 :(得分:1)

在Python中,缩进没有大括号和代码块的开始/结束,因此您应该缩进Criteria criteria = new Criteria(); criteria = criteria.and("carrierCode").is("TK"); String from = "2020-02-05 00:00:00"; String to = "2020-02-05 23:59:59"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:s"); Date toDate = dateFormat.parse(to); Date fromDate = dateFormat.parse(from); criteria = criteria.andOperator(where("scheduledDepDateTime").gte(fromDate), where("scheduledDepDateTime").lte(toDate))); // Query qry = new Query(criteria); // List<SomeClassName> result = mongoTemplate.find(qry, SomeClassName.class, "collection_name"); 。它说withwith内部。

while

答案 1 :(得分:1)

protected function redirectTo() { if ((Auth::user())->user_role == 'admin') { return 'admin'; } elseif ((Auth::user())->user_role == 'staff') { return 'staff'; } elseif ((Auth::user())->user_role == 'student') { return 'student'; } else { return redirect('sign'); } }``` 之后应该总是有一个缩进

:

答案 2 :(得分:1)

有几个函数需要缩进以下代码:forwhileifwith。编译器(在python中)需要缩进才能知道哪些条件语句触发了代码行。例如,您要

    time.sleep(10)
    count += 1

总是在while的每次迭代期间执行。因此,编译器需要查看:

while count < 100000000:
    with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

    time.sleep(10)
    count += 1

否则,这两行仅在循环的with部分执行。