Python: 'break' outside loop

前端 未结 5 1795
余生分开走
余生分开走 2020-12-01 01:42

in the following python code:

narg=len(sys.argv)
print \"@length arg= \", narg
if narg == 1:
        print \"@Usage: input_filename nelements nintervals\"
           


        
5条回答
  •  情书的邮戳
    2020-12-01 02:39

    break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like

    for item in some_iterable:
        ...
        if break_condition():
            break 
    

    The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.

提交回复
热议问题