How do I create a line-break in Terminal?

后端 未结 6 2233
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 05:48

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

6条回答
  •  抹茶落季
    2021-02-08 06:26

    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.

提交回复
热议问题