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.
我在Mac OSX最新版的终端中使用Python。当我按enter键时,它会处理我输入的代码,并且我无法知道如何添加额外的代码行,例如一个基本的循环。
5 个解决方案
#1
18
In the python shell, if you are typing code that allows for continuation, pressing enter once should not execute the code...
在python shell中,如果您正在输入允许延续的代码,那么按一次enter不应该执行代码……
The python prompt looks like this:
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:
如果您启动for循环或输入python希望从您那里得到更多的内容,提示符应该更改为elipse。例如:
>>> 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:
以下是python在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:
如果您想强迫python不要评估您输入的代码,您可以在每一行的末尾添加一个“\”……例如:
>>> def hello():\
... print "hello"\
... \
... \
... \
...
...
>>> hello()
hello
>>> hello()\
... \
... \
...
hello
>>>
hope that helps.
希望有帮助。
#2
2
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 .
我总是一次又一次地得到这三个点,却无法把它合上。它实际上是换行,并与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
>>>
#3
0
The statements which represent a block of code below end with a colon(:) in Python.
下面表示代码块的语句以Python中的冒号(:)结尾。
By doing that way, you can add extra statements under a single block and execute them at once.
通过这种方式,您可以在单个块下添加额外的语句并立即执行它们。
#4
0
The answer here is far more simple. If you want to continue in the next line after a loop like
这里的答案要简单得多。如果你想在循环之后继续下一行
while b<1:
而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键的空格进行“缩进”,只有这样你才能在三个点之后放入更多的代码
... (tab or space) print b
…(制表或空格)打印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
然后当你按下enter时代码不会被执行但是你会得到另一个…在哪里可以通过创建新的缩进来继续输入代码
keep the indent the same
保持缩进
that is it
就是这样
#5
-2
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.
顺便提一下,您的问题似乎是,您试图在常规shell提示符而不是在python shell中执行python命令。
Did you type "python" as the first step? For example:
第一步是输入“python”吗?例如:
$ 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.
>>>
#1
18
In the python shell, if you are typing code that allows for continuation, pressing enter once should not execute the code...
在python shell中,如果您正在输入允许延续的代码,那么按一次enter不应该执行代码……
The python prompt looks like this:
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:
如果您启动for循环或输入python希望从您那里得到更多的内容,提示符应该更改为elipse。例如:
>>> 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:
以下是python在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:
如果您想强迫python不要评估您输入的代码,您可以在每一行的末尾添加一个“\”……例如:
>>> def hello():\
... print "hello"\
... \
... \
... \
...
...
>>> hello()
hello
>>> hello()\
... \
... \
...
hello
>>>
hope that helps.
希望有帮助。
#2
2
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 .
我总是一次又一次地得到这三个点,却无法把它合上。它实际上是换行,并与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
>>>
#3
0
The statements which represent a block of code below end with a colon(:) in Python.
下面表示代码块的语句以Python中的冒号(:)结尾。
By doing that way, you can add extra statements under a single block and execute them at once.
通过这种方式,您可以在单个块下添加额外的语句并立即执行它们。
#4
0
The answer here is far more simple. If you want to continue in the next line after a loop like
这里的答案要简单得多。如果你想在循环之后继续下一行
while b<1:
而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键的空格进行“缩进”,只有这样你才能在三个点之后放入更多的代码
... (tab or space) print b
…(制表或空格)打印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
然后当你按下enter时代码不会被执行但是你会得到另一个…在哪里可以通过创建新的缩进来继续输入代码
keep the indent the same
保持缩进
that is it
就是这样
#5
-2
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.
顺便提一下,您的问题似乎是,您试图在常规shell提示符而不是在python shell中执行python命令。
Did you type "python" as the first step? For example:
第一步是输入“python”吗?例如:
$ 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.
>>>