Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在端子中创建换行符?_Python_Macos_Terminal - Fatal编程技术网

Python 如何在端子中创建换行符?

Python 如何在端子中创建换行符?,python,macos,terminal,Python,Macos,Terminal,我最新在MacOSX终端上使用Python。当我按enter键时,它会处理我输入的代码,我不知道如何添加额外的代码行,例如基本循环。在python shell中,如果键入允许继续的代码,按enter键一次不应执行代码 python提示符如下所示: >>> ... 如果启动for循环或键入python对您期望更高的内容,则提示应该更改为elipse。例如: >>> def hello(): or >>> for x in range(10)

我最新在MacOSX终端上使用Python。当我按enter键时,它会处理我输入的代码,我不知道如何添加额外的代码行,例如基本循环。

在python shell中,如果键入允许继续的代码,按enter键一次不应执行代码

python提示符如下所示:

>>>
...
如果启动for循环或键入python对您期望更高的内容,则提示应该更改为elipse。例如:

>>> def hello():
or
>>> for x in range(10):
>>> def hello():\
...     print "hello"\
... \
... \
... \
... 
... 
>>> hello()
hello
>>> hello()\
... \
... \
... 
hello
>>> 
$ 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.
>>> 
您应该将提示变成这样

...
这意味着它正在等待您输入更多以完成代码

下面是python在求值之前自动等待更多输入的两个完整示例:

>>> 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不计算您正在键入的代码,您可以在每行末尾附加一个“\”。。。例如:

>>> def hello():
or
>>> for x in range(10):
>>> def hello():\
...     print "hello"\
... \
... \
... \
... 
... 
>>> hello()
hello
>>> hello()\
... \
... \
... 
hello
>>> 
$ 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.
>>> 

希望对您有所帮助。

在Python中,下面表示代码块的语句以冒号(:)结尾


通过这种方式,您可以在单个块下添加额外的语句,并立即执行它们。

按照您提问的措辞,您似乎在尝试在常规shell提示符下而不是在python shell中执行python命令

第一步是键入“python”吗?例如:

>>> def hello():
or
>>> for x in range(10):
>>> def hello():\
...     print "hello"\
... \
... \
... \
... 
... 
>>> hello()
hello
>>> hello()\
... \
... \
... 
hello
>>> 
$ 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.
>>> 

这里的答案要简单得多。如果你想在下一行之后继续循环,就像


而b我总是一次又一次地得到这三个点,却无法将其闭合。它实际上是换行的,可以使用2回车键。我做到了,我试着按了两次回车键,结果成功了

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

如果您希望使用循环,正如其他人指出的,行末尾的
会将提示更改为如下所示:

>>>
...
只是想补充一点,如果您正在键入一长行代码,并且出于美观的原因想将其拆分,那么点击
shift
+
enter
将强制解释器使用
提示符将您带到新行

从那里,输入代码的其余部分并像使用循环或
if
语句一样执行,代码将按预期执行

下面是SQLAlchemy教程中利用此行为的代码片段:

>>> 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')])

要重新创建,您可以在第一行之后使用
shift
+
enter
,以便能够在新行中创建第一个
User
对象。一旦进入
,一个简单的
输入
按钮将为您提供另一行
提示。要退出,只需在该提示符下点击
enter
,返回到
提示符。

行的开头是“for”或“while”,结尾是冒号?如果在复合语句末尾添加“:”,解释器应更改其提示符,让您知道它正在等待更多输入。但是,您必须遵守Python缩进规则。换句话说,如果您想执行代码,您需要输入两次。否则,一次输入只会创建一个新行,然后可以缩进该行。