Input() in Python3

Mel mwilson at the-wire.com
Fri Apr 22 10:08:20 EDT 2011


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.




More information about the Python-list mailing list