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.
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.
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.
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 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.
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
.
raw_input will work in the notebook in IPython 1.0, pending this pull request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With