我一直在使用输入函数作为暂停脚本的一种方式:

print("something")
wait = input("Press Enter to continue.")
print("something")

有正式的方式吗?


当前回答

为了跨Python 2/3兼容性,你可以通过六个库使用输入:

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

其他回答

仅适用于Windows,请使用:

import os
os.system("pause")
print ("This is how you pause")

input()

为了跨Python 2/3兼容性,你可以通过六个库使用输入:

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

我认为停止执行的最好方法是time.sleep()函数。

如果你只需要在某些情况下暂停执行,你可以简单地实现一个If语句,就像这样:

if somethinghappen:
    time.sleep(seconds)

您可以让else分支为空。

非常简单:

raw_input("Press Enter to continue ...")
print("Doing something...")