简体   繁体   English

Python循环控制器继续无法正常工作

[英]Python loop controler continue is not working properly

As I know, "continue" will jump back to the top of the loop. 据我所知,“继续”将跳回到循环的顶部。 But in my case it's not jumping back, continue don't like me :( 但就我而言,它并没有退缩,继续不喜欢我:(

for cases in files:
  if ('python' in cases.split()):
    execute_python_scripts(cases.split())
    elif run_test_case(cases.split()):
      continue
    else:
      logger("I am here")
      break 

In my case run_test_case() gives 1, 2, 3, 4 etc... But it always performs first(1) and jump to the else part. 在我的情况下, run_test_case()给出1、2、3、4等。但是它总是执行first(1)并跳转到else部分。 So I am getting the "I am here" message. 因此,我收到“我在这里”消息。 It should not work like this. 它不应该这样工作。 As I am using "continue", it should jump to the for loop. 当我使用“ continue”时,它应该跳到for循环。

Following is the run_test_case() : 以下是run_test_case()

def run_test_case(job):
  for x in job:
    num_of_cases = num_of_cases - 1
    test_type = x.split('/')
    logger(log_file,"Currently "+ x +"hai!!")
    if test_type[0] == 'volume':
      backdoor = test_type[1].split("_")
      if backdoor[0] == 'backdoor':
        return get_all_nas_logs()
      else:
        if perform_volume_operations(x,num_of_cases) == False:
          return False
        else:
      logger(log_file,"wrong ha!!")

Why is it always going to the else part, without jumping back to the for loop? 为什么总是跳到其他部分,而又不跳回for循环? Thanks in advance. 提前致谢。

Here elif run_test_case(cases.split()): you are calling the run_test_case method, that will run your code to evaluate the result for the elif condition. 这里是elif run_test_case(cases.split()):您正在调用run_test_case方法,该方法将运行您的代码以评估elif条件的结果。

It only enters the block delimited by elif (in your case, continue ), if the result of that method evaluates to True , otherwise it will jump to the else clause. 如果该方法的结果评估为True ,则仅进入由elif分隔的块(在您的情况下为continue ),否则它将跳转到else子句。

The problem is probably in your run_test_case code, that is never returning True, and so you'll never get the behavior that you're expecting. 问题可能出在您的run_test_case代码中,它永远不会返回True,因此您永远都不会得到期望的行为。

It's hard to say without knowing exactly what you want to accomplish, but I'd say that you're missing a return True in the end of that code, meaning, if everything executes correctly right until the end, you want it to return True... but I'm only guessing here, you need to think about what that method is supposed to do. 在不确切知道要完成的工作的情况下很难说,但是我要说的是,您在该代码的末尾缺少return True ,这意味着,如果一切都正确地执行到最后,您希望它返回True ...但是我只是在这里猜测,您需要考虑该方法应该执行的操作。

In python an if or elif clause is evaluated for not only the True and False constants, but more generally for True-like and False-like values. 在python中, ifelif子句不仅针对TrueFalse常量,而且更一般地针对类似True和False的值进行评估。 None , for instance, is a false-like value, a non-empty string is a true-like value, etc. 例如, None是类似假的值,非空字符串是类似真的值,等等。

Check this from the documentation on values that are considered true or false: 从文档中检查被认为是或否的值:

http://docs.python.org/library/stdtypes.html http://docs.python.org/library/stdtypes.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM