Newbie question about evaluating raw_input() responses
Chris Angelico
rosuav at gmail.com
Wed May 22 03:19:06 EDT 2013
On Wed, May 22, 2013 at 4:52 PM, Kevin Xi <kevin.xgr at gmail.com> wrote:
> On Wednesday, May 22, 2013 2:23:15 PM UTC+8, C. N. Desrosiers wrote:
>> age=raw_input('Enter your age: ')
>> if age > 18:
>
> You can either use `raw_input` to read data and convert it to right type, or use `input` to get an integer directly. Read this: http://docs.python.org/2/library/functions.html#raw_input
> http://docs.python.org/2/library/functions.html#input
No! No, please do NOT use input()! It does not return an integer; it
*evaluates* (that is, executes) the input.
>>> input('Enter your age: ')
Enter your age: 18
18
>>> input('Enter your age: ')
Enter your age: 1+2+4+5+6
18
>>> input('Enter your age: ')
Enter your age: sys.stdout.write("Hello, world!\n") or 18
Hello, world!
18
>>> input('Enter your age: ')
Enter your age: sys.exit(0)
This is almost certainly NOT what you want to have in your script. If
you want an integer, just pass it through int() as Fabio suggested.
Please do not use, or advocate using, this steam-powered Izzet goblin
hammer for cracking walnuts.
ChrisA
More information about the Python-list
mailing list