Quick help for a python newby, please

Chris Angelico rosuav at gmail.com
Wed Nov 23 22:49:27 EST 2016


On Thu, Nov 24, 2016 at 2:41 PM, Wildman via Python-list
<python-list at python.org> wrote:
> Point taken.  I did miss the python3 part.
>
> I switched to raw_input because it handles an empty
> input.  An empty input would trigger the ValueError.
> No doubt with the correct code the same or similar
> could be done with input().  My lack of experience
> caused me to look for simpler solution and perhaps
> the wrong one.

The exact same thing is true of input() in Python 3. In Python 2,
input() is the same as eval(raw_input()), so don't use it ever [1].
You can happily use input() in Py3, even if you get empty input.

I like to put this at the top of cross-version scripts:

try: input = raw_input
except NameError: pass

Then you can proceed to use input() without worries.

ChrisA

[1] Yes, I'm aware there are times when evalling the user's input is
what you want. In those cases, be explicit and use eval.



More information about the Python-list mailing list