equivalent of raw_input in Ipython notebook
Solution 1
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
.
Solution 2
raw_input will work in the notebook in IPython 1.0, pending this pull request.
Related videos on Youtube
Ryan Saxe
Updated on September 15, 2022Comments
-
Ryan Saxe over 1 year
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.
-
Eric Brown almost 10 yearsCan you add a bit of explanation as to why this works?
-
drevicko almost 10 yearsAs far as I'm aware, the tk dialog will appear on the computer running the ipython server, and then only if the session that launched it is associated with a screen. If you're looking at the notebook remotely, you won't see the dialog..
-
dashesy about 6 yearsSo IOW, always use raw_input instead of input?
-
Pietro Battiston about 6 years@dashesy No! Rather use
input
(see the last line I added above), and use the snippet when you need backward compatbility with python 2. -
dashesy about 6 yearsyes that makes more sense. In ipython (Python2)
input
does the wrong thing (of course unless we do input=raw_input`, which confused me.