Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45716

Re: Newbie question about evaluating raw_input() responses

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'advocate': 0.07; 'wednesday,': 0.07; '22,': 0.09; 'type,': 0.09; 'subject:question': 0.10; '18:': 0.16; 'fabio': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'sys.exit(0)': 0.16; 'using,': 0.16; 'world!': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'certainly': 0.24; 'integer': 0.24; 'script.': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'kevin': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:python': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'convert': 0.35; 'no,': 0.35; 'received:google.com': 0.35; 'url:org': 0.36; 'received:209': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'read': 0.60; '(that': 0.65; 'age': 0.80; 'goblin': 0.84; 'hammer': 0.84; 'directly.': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=R8uC2Wid18kZX4Sj47+MqPxrh5DR1lD8wiKM9ecQdbc=; b=TPo/z1VS9w/7K/8GkQ13qp795NnVqomVbWikj5gXf0TuZZV0sNQYbUznSFT5NH63AT f1LXPJmuCbnb121gpbfpuvsNx3lDU28vG3AYGoX8Ew5yFu5kuvISQadVGQ0Q0t/tlkGJ rhaPvD7bh+APsfgDSAlGGyJ3M9kLuB3B2KCEbLvQKFpnoaLLIJfskil17J5LGZGn+sud 5OiM7c7YsJtYW/RwFFtcAdUBd84Mwjt0EBcPIey3L7JC+Kt+K9bgcXrsP2anVDif4qcu dxuOXeMRdpAF7ZlJX3YS+FPw6stWEV8gA1YgG5qCASMe8+AXv4+xR/p6T3xKhOX9w4Dj E8kQ==
MIME-Version 1.0
X-Received by 10.220.106.74 with SMTP id w10mr2363735vco.32.1369207146151; Wed, 22 May 2013 00:19:06 -0700 (PDT)
In-Reply-To <c8d5972d-9b11-4885-a68d-6ce1d0414718@googlegroups.com>
References <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <c8d5972d-9b11-4885-a68d-6ce1d0414718@googlegroups.com>
Date Wed, 22 May 2013 17:19:06 +1000
Subject Re: Newbie question about evaluating raw_input() responses
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1956.1369207155.3114.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369207155 news.xs4all.nl 15915 [2001:888:2000:d::a6]:46852
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45716

Show key headers only | View raw


On Wed, May 22, 2013 at 4:52 PM, Kevin Xi <kevin.xgr@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie question about evaluating raw_input() responses "C. N. Desrosiers" <cndesrosiers@gmail.com> - 2013-05-21 23:23 -0700
  Re: Newbie question about evaluating raw_input() responses Fábio Santos <fabiosantosart@gmail.com> - 2013-05-22 07:35 +0100
    Re: Newbie question about evaluating raw_input() responses "C. N. Desrosiers" <cndesrosiers@gmail.com> - 2013-05-21 23:52 -0700
  Re: Newbie question about evaluating raw_input() responses Kevin Xi <kevin.xgr@gmail.com> - 2013-05-21 23:52 -0700
    Re: Newbie question about evaluating raw_input() responses Chris Angelico <rosuav@gmail.com> - 2013-05-22 17:19 +1000
    Re: Newbie question about evaluating raw_input() responses Alister <alister.ware@ntlworld.com> - 2013-05-22 22:31 +0000
      RE: Newbie question about evaluating raw_input() responses Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-23 01:55 +0300
      Re: Newbie question about evaluating raw_input() responses Kevin Xi <kevin.xgr@gmail.com> - 2013-05-22 18:56 -0700
      Re: Newbie question about evaluating raw_input() responses Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-23 04:47 +0000
        Re: Newbie question about evaluating raw_input() responses Chris Angelico <rosuav@gmail.com> - 2013-05-23 16:04 +1000
        Re: Newbie question about evaluating raw_input() responses Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-23 03:11 -0400
        Re: Newbie question about evaluating raw_input() responses Chris Angelico <rosuav@gmail.com> - 2013-05-23 17:20 +1000
          Re: Newbie question about evaluating raw_input() responses Nobody <nobody@nowhere.com> - 2013-05-25 19:27 +0100
            Re: Newbie question about evaluating raw_input() responses Chris Angelico <rosuav@gmail.com> - 2013-05-26 04:33 +1000
            Re: Newbie question about evaluating raw_input() responses Fábio Santos <fabiosantosart@gmail.com> - 2013-05-25 23:11 +0100
      Re: Newbie question about evaluating raw_input() responses Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-23 18:51 -0400

csiph-web