如何在python脚本中暂停和等待命令输入

FJD*_*JDU 16 python

是否有可能在python中有如下的脚本?

...
Pause
->
Wait for the user to execute some commands in the terminal (e.g. 
  to print the value of a variable, to import a library, or whatever).
The script will keep waiting if the user does not input anything.
->
Continue execution of the remaining part of the script
Run Code Online (Sandbox Code Playgroud)

本质上,脚本暂时将控件提供给python命令行解释器,并在用户以某种方式完成该部分后继续.

编辑:我提出的(灵感来自答案)如下所示:

x = 1

i_cmd = 1
while True:
  s = raw_input('Input [{0:d}] '.format(i_cmd))
  i_cmd += 1
  n = len(s)
  if n > 0 and s.lower() == 'break'[0:n]:
    break
  exec(s)

print 'x = ', x
print 'I am out of the loop.'
Run Code Online (Sandbox Code Playgroud)

Cam*_*arr 28

如果您使用的是python 2.x: raw_input()

python 3.x: input()

例:

# do some stuff in script
variable = raw_input('input something!: ')
# do stuff with variable
Run Code Online (Sandbox Code Playgroud)


And*_*enz 6

我知道这样做的最好方法是使用pdb调试器.所以说

import pdb
Run Code Online (Sandbox Code Playgroud)

在你的程序的顶部然后使用

pdb.set_trace()
Run Code Online (Sandbox Code Playgroud)

对于"暂停"在(Pdb)提示符下,您可以输入诸如的命令

(Pdb) print 'x = ', x
Run Code Online (Sandbox Code Playgroud)

你也可以单步执行代码,虽然这不是你的目标.完成后只需键入即可

(Pdb) c 
Run Code Online (Sandbox Code Playgroud)

或"继续"一词的任何子集,代码将继续执行.

截至2015年11月,对调试器的一个很好的简单介绍是 https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/ 但是如果你谷歌'python调试器'当然有很多这样的来源或'python pdb'.