如何在终端中创建换行符? [英] How do I create a line-break in Terminal?

查看:239
本文介绍了如何在终端中创建换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最新在Mac OSX的Terminal中使用Python.当我按Enter键时,它会处理我输入的代码,但我无法弄清楚如何添加另一行代码,例如进行基本循环.

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.

推荐答案

在python shell中,如果您键入允许继续的代码,则按一次Enter不应执行该代码...

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

python提示符如下:

The python prompt looks like this:

>>>

如果启动for循环或在python期望您输入更多内容的地方键入提示,则提示应更改为椭圆形.例如:

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):

您的提示应变为

...

表示它正在等待您输入更多信息以完成代码.

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

以下是python的几个完整示例,它们在撤离前自动等待更多输入:

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
>>>

如果要强制python不评估正在输入的代码,可以在每行的末尾附加一个"\" ...例如:

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
>>> 

希望有帮助.

这篇关于如何在终端中创建换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆