Copy-paste into Python interactive interpreter and indentation

前端 未结 8 1671
借酒劲吻你
借酒劲吻你 2020-12-06 00:41

This piece of code, file test.py,

if 1:
   print \"foo\"
print \"bar\"

can be successfully executed with execfile(\"test.py\

8条回答
  •  温柔的废话
    2020-12-06 01:13

    Continuation lines are needed when entering a multi-line construct. --Interactive mode, The Python Tutorial (v2) (v3)

    So you need to enter:

    if 1:
       print "foo"
    
    print "bar"
    

    I've yet to find a suitable explanation as to why it's different to a non-interactive session, alas.

提交回复
热议问题