在Python交互模式下获取输入



假设我有一个简单的脚本,它取决于我的输入:

w = input()
print(f'Input is {w}')

如果我将此脚本(两行同时(复制并粘贴到交互式窗口中,它将不会在input行上暂停以接收输入。

>>> w = input()
print(f'Input is {w}')
>>>

有什么办法可以改变这种行为吗?

更新:这似乎在Pycharm:上运行得很好

In: w = input()
print(f'Input is {w}')
>? test
Input is test

您可以使用IPython,它支持粘贴块:

In [1]: w = input()
...: print(f'Input is {w}')
a
Input is a

如果不起作用,您可以使用命令%paste加载并执行剪贴板内容,或者使用%cpaste手动粘贴:

In [2]: %paste
w = input()
print(f'Input is {w}')
## -- End pasted text --
b
Input is b
In [4]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:w = input()
:print(f'Input is {w}')
:--
ERROR! Session/line number was not unique in database. History logging moved to new session 1903
c
Input is c

(我不确定这个错误意味着什么BTW,尽管我注意到"In"数字比它应该有的多勾了一次。(

另请参阅:将多行代码段粘贴到IPython 中

最新更新