[Tutor] input python 3.3
Peter Otten
__peter__ at web.de
Tue Feb 4 10:18:07 CET 2014
Ian D wrote:
> Hello
>
> I used to use 2.7 and the input was pretty when inputting a numeric value,
> it would just get cast to an int.
>
> Seems that 3.3 I have to cast each input so :
> float(num1 = input("Enter a number")
You mean
num1 = float(input("Enter a number"))
> Is this just they way it is now? Is there a way to get back to just
> typing:
> num1 = input("Enter a number ")
> in python 33.
>
> Seems a backwards step unless (and am sure this is the case)it is
> beneficial in so many other ways?
input() in Python 2 did not just recognize numbers, it allowed you to
evaluate an arbitrary Python expression. For example:
$ touch important_file
$ ls
important_file
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input("Enter a number: ")
Enter a number: __import__("os").remove("important_file")
>>>
$ ls
Oops, the file is gone. I'm sure you can see why this is dangerous.
Therefore the recommendation for Python 2 is to use raw_input() instead of
input() -- and for Python 3 raw_input() was renamed to the more obvious
input().
You can get the old input() behaviour with
num1 = eval(input(...))
which is still dangerous, but makes the risk more obvious.
More information about the Tutor
mailing list