2021年3月11日星期四

How to use input() in python script for Jupyter notebooks

I can't figure out how to correctly use input() to use in Jupyter Notebooks. Unfortunately, I cannot use widgets and need a workaround. This is is what I've tried:

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")  

When I try to run in Jupyter notebooks, the input does not wait for the user input and outputs EOF when reading a line. How can I make it wait for the user input? Thanks in advance

enter image description here

Edit: I am using Python 3.7.6, so raw_input() is not defined, and it is actually input().

https://stackoverflow.com/questions/66592730/how-to-use-input-in-python-script-for-jupyter-notebooks March 12, 2021 at 09:00AM

没有评论:

发表评论