简体   繁体   中英

Python Unusual error with multiline code in IDLE Shell

I was testing some code on IDLE for Python which I haven't used in a while and stumbled on an unusual error.

I was attempting to run this simple code:

for i in range(10):
    print(i)
print('Done')

I recall that the shell works on a line by line basis, so here is what I did first:

>>> for i in range(10):
        print(i)
    print('Done')

This resulted in a indent error, shown by the picture below:

缩进IDLE错误

I tried another way, as it might be that the next statement needed to be at the start perhaps, like this:

>>> for i in range(10):
        print(i)
print('Done')

But this gave a syntax error, oddly:

第二个错误,IDLE的语法错误

This is quite odd to me the way IDLE works.

Take Note: I am actually testing a much more complex program and didn't want to create a small Python file for a short test. After all, isn't IDLE's shell used for short tests anyways?

Why is multi-line coding causing this issue? Thanks.

Just hit return once or twice after the print(i) , until you get the >>> prompt again. Then you can type the print('Done') . What's going on is that python is waiting for you to tell it that you're done working inside that for . And you do that by hitting return .

(You'll see, though, that the for loop is executed right away.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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