Input() in Python3
Westley MartÃnez
anikom15 at gmail.com
Sat Apr 23 15:33:47 EDT 2011
On Fri, Apr 22, 2011 at 10:08:20AM -0400, Mel wrote:
> Westley MartÃnez wrote:
> > On Fri, Apr 22, 2011 at 04:49:19PM +1000, Chris Angelico wrote:
>
> >> 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.
>
> But sys.exit() doesn't return a string. My fave is
>
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> a = int (input ('enter a number >'))
> enter a number >sys.setrecursionlimit(1)
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Error in sys.excepthook:
> RuntimeError: maximum recursion depth exceeded
>
> Original exception was:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> RuntimeError: maximum recursion depth exceeded while calling a Python object
> >>> int (0)
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Error in sys.excepthook:
> RuntimeError: maximum recursion depth exceeded
>
> Original exception was:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> RuntimeError: maximum recursion depth exceeded while calling a Python object
> >>>
>
>
> Mel.
>
What?
More information about the Python-list
mailing list