How do I create a line-break in Terminal?

Question:

I’m using Python in Terminal on Mac OSX latest. When I press enter, it processes the code I’ve entered, and I am unable to figure out how to add an additional line of code e.g. for a basic loop.

Asked By: jhealy

||

Answers:

In the python shell, if you are typing code that allows for continuation, pressing enter once should not execute the code…

The python prompt looks like this:

>>>

If you start a for loop or type something where python expects more from you the prompt should change to an elipse. For example:

>>> def hello():
or
>>> for x in range(10):

you the prompt should turn into this

...

meaning that it is waiting for you to enter more to complete the code.

Here are a couple complete examples of python automatically waiting for more input before evauluation:

>>> def hello():
...    print "hello"
...
>>> hello()
hello
>>>
>>> for x in range(10):
...     if x % 2:
...         print "%s is odd" % (x,)
...     else:
...         print "%s is even" % (x,)
... 
0 is even
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
>>>

If you want to force python to not evaluate the code you are typing you can append a “” at the end of each line… For example:

>>> def hello():
...     print "hello"
... 
... 
... 
... 
... 
>>> hello()
hello
>>> hello()
... 
... 
... 
hello
>>> 

hope that helps.

Answered By: dting

The statements which represent a block of code below end with a colon(:) in Python.

By doing that way, you can add extra statements under a single block and execute them at once.

Answered By: tamizhgeek

It almost sounds, by the way you’ve worded your question, that you’re trying to execute your python commands at the regular shell prompt rather than within the Python shell.

Did you type “python” as the first step? For example:

$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Answered By: btanaka

The answer here is far more simple. If you want to continue in the next line after a loop like

while b<1:

when you press enter you get prompted with

then you “have to make an indent” by space of tab and only then you can put more code after the three dots like

… (tab or space) print b

then when you press enter the code is not going to be executed but you get another … where you can keep typing you code by making the new indent

keep the indent the same

that is it

Answered By: stdavid

I was always getting those three dots again and again and could not close it . Its actually Line break and works with 2 ENTER. I did it I tried giving two times ENTER key and it worked .

>>> primenumlist = [2,3,5,7,11,13,17,19,23,29]
>>> for i in primenumlist:
...  print (i)
...
2
3
5
7
11
13
17
19
23
29
>>>
Answered By: Seth Projnabrata

In case you’re looking to use a loop, the : at the end of the line, as other pointed, out will change your prompt to one that looks like this:

...

Just wanted to add that in case you’re typing a long line of code and wanted to break it up for aesthetic reasons, hitting shift + enter forces the interpreter to take you to a new line with the ... prompt.

From there, type in the rest of your code and execute as you would with a loop or if statement and your code will execute as expected!

Here’s a code snippet from a SQLAlchemy tutorial that exploits this behavior:

>>> session.add_all([
...     User(name='wendy', fullname='Wendy Williams', password='foobar'),
...     User(name='mary', fullname='Mary Contrary', password='xxg527'),
...     User(name='fred', fullname='Fred Flinstone', password='blah')])

To recreate this, you’d use shift + enter after the first line to be able to create the first User object in a new line. Once at the ..., a simple enter press will give you another line with the ... prompt. To exit, simply hit enter at that prompt to return to the > prompt.

Answered By: Prratek Ramchandani

it’s very simple.

>>> def abc(a):

after type ":" in this funtion you can just press "Enter" button.
you will see your poiter will be display in with line break like below.

>>> def abc(a):
...

and later every time when you press enter after you complete the statement.
you will go to next line like this.

>>> def abc(a, b):
...     print(a+b)
...

After you want to exit , then press enter in the next line beginning like this

>>> def abc(a,b):
...     print(a+b)
... 
>>> 

Hope you find this useful.

Answered By: Malith Hirantha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.