I've been using the input function as a way to pause my scripts
我一直在使用输入函数来暂停我的脚本
print("something")
wait = input("PRESS ENTER TO CONTINUE.")
print("something")
is there a formal way to do this?
有正式的方法吗?
10 个解决方案
#1
154
Seems fine to me (or raw_input()
in Python 2.X). Alternatively you could use time.sleep()
if you want to pause for a certain number of seconds.
对我来说似乎很好(或Python 2.X中的raw_input())。或者,如果要暂停一定的秒数,可以使用time.sleep()。
import time
print("something")
time.sleep(5.5) # pause 5.5 seconds
print("something")
#2
20
use:
使用:
import os
os.system("pause")
#3
#4
8
So, I found this to work very well in my coding endeavors. I simply created a function at the very beginning of my program,
所以,我发现这在我的编码工作中非常有效。我只是在程序的最开始创建了一个函数,
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
and now I can use the pause()
function whenever I need to just as if I was writing a batch file. For example, in a program such as this:
现在我可以使用pause()函数,就像我在编写批处理文件一样。例如,在这样的程序中:
import os
import system
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
print("Think about what you ate for dinner last night...")
pause()
Now obviously this program has no objective and is just for example purposes, but you can understand precisely what I mean.
现在显然这个程序没有目标,只是为了举例,但你可以准确理解我的意思。
NOTE: For Python 3, you will need to use input
as opposed to raw_input
注意:对于Python 3,您将需要使用输入而不是raw_input
#5
5
Very simple:
很简单:
raw_input("Press Enter to continue ...")
exit()
#6
4
I have had a similar question and I was using signal:
我有一个类似的问题,我正在使用信号:
import signal
def signal_handler(signal_number, frame):
print "Proceed ..."
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
So you register a handler for the signal SIGINT and pause waiting for any signal. Now from outside your program (e.g. in bash), you can run kill -2 <python_pid>
, which will send signal 2 (i.e. SIGINT) to your python program. Your program will call your registered handler and proceed running.
因此,您为信号SIGINT注册处理程序并暂停等待任何信号。现在从你的程序外部(例如在bash中),你可以运行kill -2
#7
4
As pointed out by mhawke and steveha's comments, the best answer to this exact question would be:
正如mhawke和steveha的评论所指出的,这个问题的最佳答案是:
For a long block of text, it is best to use
input('Press <ENTER> to continue')
(orraw_input('Press <ENTER> to continue')
on Python 2.x) to prompt the user, rather than a time delay. Fast readers won't want to wait for a delay, slow readers might want more time on the delay, someone might be interrupted while reading it and want a lot more time, etc. Also, if someone uses the program a lot, he/she may become used to how it works and not need to even read the long text. It's just friendlier to let the user control how long the block of text is displayed for reading.对于长文本块,最好使用输入('按
继续')(或在Python 2.x上使用raw_input('按 继续')来提示用户,而不是时间延迟。快速读者不想等待延迟,慢读者可能希望有更多时间延迟,有人可能在阅读时被打断并且想要更多时间等等。另外,如果有人使用该程序很多,他/她可能已经习惯了它的工作原理,甚至不需要读长篇文章。让用户控制文本块显示多长时间以便阅读会更友好。
#8
3
I use the following for python
2 and 3 to pause code execution until user presses ENTER
我使用以下python 2和3来暂停代码执行,直到用户按下ENTER
import six
if six.PY2:
raw_input("Press the <ENTER> key to continue...")
else:
input("Press the <ENTER> key to continue...")
#9
3
Print ("This is how you pause")
input()
#10
0
i think that the best way to stop the execution is the time.sleep() function. if you need to suspend the execution only in certain cases you can simply implement an if statement like this:
我认为停止执行的最佳方法是time.sleep()函数。如果你只需要在某些情况下暂停执行,你可以简单地实现这样的if语句:
if somethinghappen:
time.sleep(seconds)
you can leave the else branch empty.
你可以把else分支留空。
#1
154
Seems fine to me (or raw_input()
in Python 2.X). Alternatively you could use time.sleep()
if you want to pause for a certain number of seconds.
对我来说似乎很好(或Python 2.X中的raw_input())。或者,如果要暂停一定的秒数,可以使用time.sleep()。
import time
print("something")
time.sleep(5.5) # pause 5.5 seconds
print("something")
#2
20
use:
使用:
import os
os.system("pause")
#3
#4
8
So, I found this to work very well in my coding endeavors. I simply created a function at the very beginning of my program,
所以,我发现这在我的编码工作中非常有效。我只是在程序的最开始创建了一个函数,
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
and now I can use the pause()
function whenever I need to just as if I was writing a batch file. For example, in a program such as this:
现在我可以使用pause()函数,就像我在编写批处理文件一样。例如,在这样的程序中:
import os
import system
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
print("Think about what you ate for dinner last night...")
pause()
Now obviously this program has no objective and is just for example purposes, but you can understand precisely what I mean.
现在显然这个程序没有目标,只是为了举例,但你可以准确理解我的意思。
NOTE: For Python 3, you will need to use input
as opposed to raw_input
注意:对于Python 3,您将需要使用输入而不是raw_input
#5
5
Very simple:
很简单:
raw_input("Press Enter to continue ...")
exit()
#6
4
I have had a similar question and I was using signal:
我有一个类似的问题,我正在使用信号:
import signal
def signal_handler(signal_number, frame):
print "Proceed ..."
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
So you register a handler for the signal SIGINT and pause waiting for any signal. Now from outside your program (e.g. in bash), you can run kill -2 <python_pid>
, which will send signal 2 (i.e. SIGINT) to your python program. Your program will call your registered handler and proceed running.
因此,您为信号SIGINT注册处理程序并暂停等待任何信号。现在从你的程序外部(例如在bash中),你可以运行kill -2
#7
4
As pointed out by mhawke and steveha's comments, the best answer to this exact question would be:
正如mhawke和steveha的评论所指出的,这个问题的最佳答案是:
For a long block of text, it is best to use
input('Press <ENTER> to continue')
(orraw_input('Press <ENTER> to continue')
on Python 2.x) to prompt the user, rather than a time delay. Fast readers won't want to wait for a delay, slow readers might want more time on the delay, someone might be interrupted while reading it and want a lot more time, etc. Also, if someone uses the program a lot, he/she may become used to how it works and not need to even read the long text. It's just friendlier to let the user control how long the block of text is displayed for reading.对于长文本块,最好使用输入('按
继续')(或在Python 2.x上使用raw_input('按 继续')来提示用户,而不是时间延迟。快速读者不想等待延迟,慢读者可能希望有更多时间延迟,有人可能在阅读时被打断并且想要更多时间等等。另外,如果有人使用该程序很多,他/她可能已经习惯了它的工作原理,甚至不需要读长篇文章。让用户控制文本块显示多长时间以便阅读会更友好。
#8
3
I use the following for python
2 and 3 to pause code execution until user presses ENTER
我使用以下python 2和3来暂停代码执行,直到用户按下ENTER
import six
if six.PY2:
raw_input("Press the <ENTER> key to continue...")
else:
input("Press the <ENTER> key to continue...")
#9
3
Print ("This is how you pause")
input()
#10
0
i think that the best way to stop the execution is the time.sleep() function. if you need to suspend the execution only in certain cases you can simply implement an if statement like this:
我认为停止执行的最佳方法是time.sleep()函数。如果你只需要在某些情况下暂停执行,你可以简单地实现这样的if语句:
if somethinghappen:
time.sleep(seconds)
you can leave the else branch empty.
你可以把else分支留空。