Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of raw_input in Ipython notebook

I am just messing around with Ipython notebook, and I was going to create a battleship game...unfortunately I need lines like the following in the code in order to make a game like that:

move = raw_input("Where would you like to attack? ")

Ipython notebook does not allow raw_input...so how could I get input from one of the players? I have searched around and nothing I could find had a direct answer to this such as no you can't or yes and this is how. Thanks.

like image 241
Ryan Saxe Avatar asked Apr 22 '13 19:04

Ryan Saxe


People also ask

What is raw_input () Python?

The raw_input() function which is available in python 2 is used to take the input entered by the user. The raw_input() function explicitly converts the entered data into a string and returns the value. The raw_input() can be used only in python 2. x version.

Does Python 3 have raw_input?

The raw_input() function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline. This page shows some common and useful raw_input() examples for new users. Please note that raw_input() was renamed to input() in Python version 3.

What is the difference between input () and raw_input ()?

There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.

What is %% capture in Jupyter?

What does %% capture do in Jupyter? Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


2 Answers

IPython 2 now supports "raw_input", IPython 3 supports "input". Notice that "input" is present in IPython 2, but it is not the same as in IPython 3! Rather it does the equivalent of eval(input( )).

(this is not a particularity of IPython, it is just behaviour inherited from Python 2/3)

If you want something portable in a notebook, just write towards the beginning of it:

try:
    input = raw_input
except NameError: #Python 3
    pass

... and then always use input.

like image 162
Pietro Battiston Avatar answered Oct 12 '22 13:10

Pietro Battiston


raw_input will work in the notebook in IPython 1.0, pending this pull request.

like image 41
minrk Avatar answered Oct 12 '22 13:10

minrk