Bluesman
Bluesman

Reputation: 19

Entering multiple lines / statements into the IDLE shell?

x=100

ENTER

if x == 10:
    print("10!")
elif x == 20:
    print("20!")
else:
    print("I don't know!")

It prints I don't know! although it's more than one line? What is the limit of the shell? What does "executed individually" mean - no matter how much code you write, it executes the first line / statement and ignores the rest?

Upvotes: 0

Views: 1268

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19174

A Python program is a sequence of statements. In IDLE's Shell, one enters and executes a single statement at a time. This is much more useful than entering a single physical line at a time, as with python.exe running in a Windows console or *nix Terminal. The text you quoted was talking about the latter, not IDLE.

To understand 'statement', we must start with 'line'. A physical line is "a sequence of characters terminated by an end-of-line sequence." A logical line can be two or more physical lines joined either explicitly using a \ character or implicitly using (), [], or {} pairs.

A simple statement comprises one logical line. A compound statement usually comprises multiple logical lines, each of which may be more than one physical line. Your if statement is an example of a compound statement.

In IDLE, one enters a complete statement on one or more physical lines. When a simple statement is complete (when one has entered a complete logical line), IDLE runs it. Since a compound statement can have an indefinite number of logical lines, one enters a blank line to indicate the end.

Upvotes: 1

Related Questions