Python command line argument semicolon-loop error

后端 未结 4 2048
猫巷女王i
猫巷女王i 2020-12-10 17:56

I was trying out python -mtimeitso I put python -mtimeit \"n = 0; while n < 10: pass\" Then an invalid syntax error showed up. same with semicol

4条回答
  •  执笔经年
    2020-12-10 18:22

    The selected answer superbly tackles the why, but not the question of how this can be worked around under any operating system (since windows cmd doesn't allow multi-line statements)

    The answer is: exec

    You have to nest any loops in an exec statement.

    Examples: (Python 2)

    python -c "i = 3; while i:print i; i-=1"
    

    is a syntax error, while

    python -c "i = 3; exec 'while i:print i;i-=1'"
    

    works correctly.

提交回复
热议问题