Input() in Python3

Westley Martínez anikom15 at gmail.com
Fri Apr 22 15:52:20 CEST 2011


On Fri, Apr 22, 2011 at 04:49:19PM +1000, Chris Angelico wrote:
> On Fri, Apr 22, 2011 at 4:22 PM, harrismh777 <harrismh777 at charter.net> wrote:
> > now we get this for input():
> >
> >   raw_input("prompt>") --> string
> 
> I would have to say that the 2.x behaviour of input() is a mistake
> that's being corrected in 3.x. With a simple name like input(), it
> should do something simple and straightforward - not eval() the
> expression.
> 
> > to:        a = eval(input("enter a number > "))
> 
> Uhhhh.... NO. NO NO NO. What if someone enters "os.exit()" as their
> number? You shouldn't eval() unchecked user input!
> 
> Chris Angelico

Right, there's no way to check you're getting a number, however using:

a = int(input('enter a number > ')) # use float() for floats

will raise an exception if it can't convert the string.



More information about the Python-list mailing list