Why doesn't exec(“break”) work inside a while loop

后端 未结 6 601
执笔经年
执笔经年 2021-01-18 13:26

As the question asks, why doesn\'t the below code work:

while True:
      exec(\"break\")

I am executing the above in pycharm via python 3.

6条回答
  •  离开以前
    2021-01-18 13:59

    exec function runs code inside a code and that means it runs out of nowhere! So, your while loop doesn't catch it. Your file is . exec runs on another file called . it doesn't recognize it where are you trying to break a loop where there is not a loop. So, your code is this:

    while True:
        exec("break")
    

    It should be like this:

    while True:
        break
    

提交回复
热议问题