【问题标题】:Nested if loop inside a for loop only being executed once嵌套在 for 循环中的 if 循环仅执行一次
【发布时间】:2022-06-15 04:38:38
【问题描述】:

下面的循环只执行一次。我的意思是,一旦满足 if 条件,它就会退出 for 循环,并且不会进一步迭代。

for loop in files:
    print("This is get pass/fail lopp",loop)
    with open(loop) as file:
        print("This is test pass/fail",loop)
        lines = file.readlines()
        error_str = json.dumps(lines)
        status_count = error_str.count("status")
        print("This is status count ", status_count, "and ", error_str)
        if status_count == 1 :
            test_passed_output = "None"
            test_failed_output = "test_setup"
            error_description = (test_setup.debug_setup(error_str))
            print("This is loop 1")
            return test_passed_output, test_failed_output, error_description


        elif status_count == 2 :
            test_passed_output = "test_setup"
            test_failed_output = "test_sanity"
            error_description = (test_sanity.debug_sanity(error_str))
            print("This is loop 2")
            return test_passed_output, test_failed_output, error_description
        # status_count == 3 :
        else:
            test_passed_output = "test_setup, test_sanity"
            test_failed_output = "test_power_cycle"
            error_description = (test_power_cycle.debug_power_cycle(error_str))
            print("This is loop 3")
            return test_passed_output, test_failed_output,error_description

【问题讨论】:

  • if loop,没有if loop这样的东西
  • 好吧,如果不是循环
  • 这是在函数内部吗?如果是这样,一旦解释器遇到return 语句,它将退出函数并且循环的其余部分不会运行。
  • if/elif/else 块内,你有return,所以我猜这是函数的一部分,它只是结束了执行。你的意思是yield
  • 不是在循环内返回,而是将值附加到列表中。然后在循环后返回列表。

标签: python


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 2012-08-15
  • 2018-07-07
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
相关资源
最近更新 更多