我不知道如何在Jupyter笔记本中正确使用input()。不幸的是,我不能使用小部件,需要一个变通方法。
这就是我尝试过的:首先,我用下面的代码创建了Jupyter的a cell:
from IPython.display import display, Markdown, Latex, Math,
clear_output
import ipywidgets as widgets
class Problem:
def __init__(self, problem_statement):
# problem_statement is some string
self.problem_statement = problem_statement
def state_problem(self, problem_statement):
title = '## ' + "New Problem"
display(Markdown(title))
display(Markdown(problem_statement))
button = widgets.Button(description="Click to Answer!")
display(button)
button.on_click(on_button_clicked)
def on_button_clicked(b):
input("Please enter a string:\n")
当我尝试在Jupyter notebooks中运行时,输入并不等待用户输入,而是在读取一行时输出EOF。
p = Problem("How many apples do we have")
Problem.state_problem(p,"How many apples do we have" )
如何让它等待用户输入?提前感谢
这就是我要运行的
编辑:我使用的是Python3.7.6,所以没有定义raw_input(),它实际上是input()。
发布于 2021-03-12 09:08:38
尝试将输入赋值给变量。我去掉了'b‘参数,因为我不确定您传递给函数的是什么
def on_button_clicked():
print("Please enter a string:\n")
x = input()
print(x)
https://stackoverflow.com/questions/66592730
复制相似问题