Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python 3 print end "" produce an exception in the interpreter but not from import?

Tags:

python

In Python 3, this works when importing from a file:

myInt = 0
while (myInt < 10):
    print (myInt, end='')
    myInt += 1
print (' are Numerals.')

producing the expected result: "0123456789 are Numerals."

But if the code is pasted directly into an interpreter, the last line produces an exception. In fact, anything after the while block exits does:

    File "<stdin>", line 4
    print (' are  Numerals.')
        ^
SyntaxError: invalid syntax

[The following is a reply to François in the form of focusing the question.]

It doesn't work in Python 2 using the trailing "," construct either...

Adding a blank line after the while block doesn't work as it clearly won't produce the desired result, namely "0123456789 are Numerals." Taking Jean-François' lead, however, this produces a similar result:

myInt = 0
res=""
while (myInt < 10):
    res += str (myInt)
    myInt += 1

res += ' are my Numerals.'
print (res)

But is there any way of forcing the end of the while block in the interpreter which would allow printing (or string compilation, or whatever) to continue? Well, I can produce the result, when I use else and enter the following, a line at a time:

>>> myInt = 0
>>> while (myInt < 10):
...     print (myInt, end='')
...     myInt += 1
... else:
...     print (' are Numerals.')
... 
0123456789 are Numerals.

but when I copy/paste the whole code into the interpreter, the exception is raised. So what is the difference between copy-pasting into the interpreter and typing it a line at a time? I'm even more curious now!

like image 587
dee Avatar asked Jul 16 '18 21:07

dee


People also ask

Why is Python running a module when I import it?

Because this is just how Python works - keywords such as class and def are not declarations. Instead, they are real live statements which are executed. If they were not executed your module would be empty.

Does importing a Python script run it?

When you import a module in Python, all the code in it will be run, and all the variables in that module will be stuck on that module object.

How do I know if a Python module is imported?

Use: if "sys" not in dir(): print("sys not imported!")

Do you need to import exception in Python?

The exceptions module provides the standard exception hierarchy. It's automatically imported when Python starts, and the exceptions are added to the _ _builtin_ _ module. In other words, you usually don't need to import this module.


Video Answer


1 Answers

The interpreter expects input in a very specific form for multi-line statements. You can see that by inputting the lines one at a time.

>>> myInt = 0
>>> while (myInt < 10):
...  print (myInt, end='')
...  myInt += 1
... print (' are Numerals')

After a multi-line statement, the interpreter expects a blank line to signify the end of the block. When it encounters a new, un-indented statement immediately, it's confused.

Adding a blank line after the end of the while loop will allow the interpreter to understand your block. Remember that the Python interpreter always runs one statement at a time. When you copy-paste multiple lines, you're really running them as completely separate statements, and in the interpreter, a multi-line statement has to be terminated by a newline.

like image 166
Silvio Mayolo Avatar answered Oct 26 '22 23:10

Silvio Mayolo