如何在python中提供jupyter单元标准输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34968112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:47:02  来源:igfitidea点击:

How to give jupyter cell standard input in python?

pythonjupyterjupyter-notebook

提问by Alex

I am trying to run a program on a jupyter notebook that accepts user input, and I cannot figure out how to get it to read standard input. For example, if I run the code with shift-enter:

我正在尝试在接受用户输入的 jupyter 笔记本上运行一个程序,但我不知道如何让它读取标准输入。例如,如果我使用 shift-enter 运行代码:

a = input()
print(a)

the cell indicates it is running, but does not accept input from me. How do I get it to accept input?

单元格表明它正在运行,但不接受我的输入。我如何让它接受输入?

采纳答案by Rahul Tripathi

Use the raw_input()(for Python 2) or input()(for Python 3) methods.

使用raw_input()(对于 Python 2)或input()(对于 Python 3)方法。

Example code:

示例代码:

a = raw_input()
print(a)

Example notebook:

示例笔记本:

回答by Alex

Restarted my computer, and everything worked fine. No idea what happened...

重新启动我的电脑,一切正常。不知道发生了什么...

回答by naeschdy

Probably you hit Shift-Enter a second time without completing the first input with Enter, so the kernel was always waiting until the first command completed, before executing it again. If you use in the menu

可能您第二次按下 Shift-Enter 键而没有使用 Enter 完成第一个输入,因此内核总是在等待第一个命令完成,然后再次执行它。如果在菜单中使用

"Kernel", "Interrupt",

“内核”、“中断”、

all active commands are stopped (including the second execution of the box) and the problem should be solved without restarting the computer (or the browser / the kernel).

停止所有活动命令(包括第二次执行该框),无需重新启动计算机(或浏览器/内核)即可解决问题。

回答by Rahul Tripathi

use raw_input instead of input if you are using python 2 version. if u still getting same problem then,

如果您使用的是 python 2 版本,请使用 raw_input 而不是 input。如果你仍然遇到同样的问题,那么

click on kernel then "restart and run all" and try to run the code again. this will fix it.

单击内核,然后“重新启动并运行全部”并尝试再次运行代码。这将解决它。

回答by Andrea Longarini

I came across the same problem, using the input in jupyternotebook, it blocks the execution and it does not work until restarting the program, so I added a print () after each input and my program is working. Hope this helps.

我遇到了同样的问题,使用jupyternotebook中的输入,它阻塞了执行,直到重新启动程序才起作用,所以我在每次输入后添加了一个print(),我的程序正在运行。希望这可以帮助。

回答by Santiago

You are doing it right, you ony have to restart the kernel (over the Run button)

你做得对,你只需要重新启动内核(通过运行按钮)