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

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

有正式的方式吗?


当前回答

跨平台的方式;工作无处不在

import os, sys

if sys.platform == 'win32':
    os.system('pause')
else:
    input('Press any key to continue...')

其他回答

跨平台的方式;工作无处不在

import os, sys

if sys.platform == 'win32':
    os.system('pause')
else:
    input('Press any key to continue...')
print ("This is how you pause")

input()

非常简单:

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

我和喜欢简单解决方案的非程序员一起工作:

import code
code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals()) 

这产生了一个几乎完全像真正的解释器一样的解释器,包括当前上下文,只有输出:

Paused. Press ^D (Ctrl+D) to continue.
>>>

Python调试器也是暂停的好方法。

import pdb
pdb.set_trace() # Python 2

or

breakpoint() # Python 3

通过这个方法,你可以通过按任何指定的键来恢复你的程序:

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'):  # The same. you can put any key you like instead of 'space'
        break

注意:你可以通过在shell或cmd中写这个命令来安装键盘模块:

pip install keyboard