问题:暂停Python程序的正确方法

我一直在使用该input功能来暂停脚本

print("something")
wait = input("PRESS ENTER TO CONTINUE.")
print("something")

有正式的方法吗?

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?


回答 0

对我来说似乎很好(或raw_input()在Python 2.X中)。或者,time.sleep()如果您想暂停一定的秒数,则可以使用。

import time
print("something")
time.sleep(5.5)    # pause 5.5 seconds
print("something")

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

import time
print("something")
time.sleep(5.5)    # Pause 5.5 seconds
print("something")

回答 1

仅适用于Windows

import os
os.system("pause")

For Windows only, use:

import os
os.system("pause")

回答 2

我假设您想暂停而不输入

time.sleep(secs)

I assume you want to pause without input.

Use:

time.sleep(seconds)


回答 3

因此,我发现这在我的编码工作中非常有效。我只是程序的开头创建了一个函数

def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

现在我可以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()

现在显然该程序没有目标,仅是示例目的,但是您可以准确地理解我的意思。

注意:对于Python 3,您需要使用input而不是raw_input

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:

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


回答 4

我有一个类似的问题,我正在使用信号:

import signal

def signal_handler(signal_number, frame):
    print "Proceed ..."

signal.signal(signal.SIGINT, signal_handler)
signal.pause()

因此,您为信号SIGINT注册了一个处理程序,并暂停等待任何信号。现在,从程序外部(例如,在bash中),您可以运行kill -2 <python_pid>,它将向您的python程序发送信号2(即SIGINT)。您的程序将调用您的注册处理程序并继续运行。

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.


回答 5

我在python2和3中使用以下命令来暂停代码执行,直到用户按下ENTER

import six
if six.PY2:
    raw_input("Press the <ENTER> key to continue...")
else:
    input("Press the <ENTER> key to continue...")

I use the following for Python 2 and Python 3 to pause code execution until user presses Enter

import six

if six.PY2:
    raw_input("Press the <Enter> key to continue...")
else:
    input("Press the <Enter> key to continue...")

回答 6

很简单:

raw_input("Press Enter to continue ...")
exit()

Very simple:

raw_input("Press Enter to continue ...")
exit()

回答 7

Print ("This is how you pause")

input()
Print ("This is how you pause")

input()

回答 8

正如mhawkesteveha的评论所指出的那样,对该确切问题的最佳答案将是:

对于较长的文本块,最好使用input('Press <ENTER> to continue')(或raw_input('Press <ENTER> to continue')在Python 2.x上)提示用户,而不是延迟时间。快速的阅读器不想等待延迟,慢速的阅读器可能希望在延迟上花费更多的时间,某人在阅读该延迟时可能会被打扰,并且需要更多的时间,依此类推。此外,如果某人经常使用该程序,他/她可能已经习惯了它的工作原理,甚至不需要阅读冗长的文字。让用户控制将文本块显示多长时间以供阅读,这是更友好的做法。

As pointed out by mhawke and steveha‘s comments, the best answer to this exact question would be:

For a long block of text, it is best to use input('Press <ENTER> to continue') (or raw_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.


回答 9

我认为停止执行的最好方法是time.sleep()函数。如果仅在某些情况下需要暂停执行,则可以简单地实现以下if语句:

if somethinghappen:
    time.sleep(seconds)

您可以将else分支留空。

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:

if somethinghappen:
    time.sleep(seconds)

You can leave the else branch empty.


回答 10

我想我喜欢这种饮料。

import getpass
getpass.getpass("Press Enter to Continue")

它隐藏了用户键入的内容,这有助于弄清此处未使用输入。

但是请注意,在OSX平台上,它显示的密钥可能会造成混淆。

就像我说的那样,它显示了一个钥匙


最好的解决方案可能是自己做与getpass模块类似的事情,而不进行read -s调用。也许使fg颜色与bg匹配?

I think I like this solution:

import getpass
getpass.getpass("Press Enter to Continue")

It hides whatever the user types in, which helps clarify that input is not used here.

But be mindful on the OS X platform. It displays a key which may be confusing.

It shows a key, like I said


Probably the best solution would be to do something similar to the getpass module yourself, without making a read -s call. Maybe making the foreground color match the background?


回答 11

通过这种方法,只需按以下指定的任何键即可恢复程序:

import keyboard
while True:
    key = keyboard.read_key()
    if key == 'space':  # you can put any key you like instead of 'space'
        break

相同的方法,但以另一种方式:

import keyboard
while True:
    if keyboard.is_pressed('space'):  # same, you can put any key you like instead of 'space'
        break

注意:您keyboard只需在shell或cmd中编写以下代码即可安装模块:

pip install keyboard

By this method, you can resume your program just by pressing any specified key you’ve specified that:

import keyboard
while True:
    key = keyboard.read_key()
    if key == 'space':  # You can put any key you like instead of 'space'
        break

The same method, but in another way:

import keyboard
while True:
    if keyboard.is_pressed('space'):  # The same. you can put any key you like instead of 'space'
        break

Note: you can install the keyboard module simply by writing this in you shell or cmd:

pip install keyboard

回答 12

为了实现跨Python 2/3的兼容性,可以input通过以下six库使用:

import six
six.moves.input( 'Press the <ENTER> key to continue...' )

For cross Python 2/3 compatibility, you can use input via the six library:

import six
six.moves.input( 'Press the <ENTER> key to continue...' )

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。