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


Groups > comp.lang.python > #32026

Re: Python input function not working in 2.7 version

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'subject:Python': 0.05; 'think,': 0.07; 'python': 0.09; '(without': 0.09; 'function),': 0.09; 'subject:2.7': 0.09; 'subject:version': 0.09; 'tuple': 0.09; 'subject:not': 0.11; 'advance.': 0.15; 'passing': 0.15; '"new-style': 0.16; '24,': 0.16; 'comma': 0.16; 'dangerous,': 0.16; 'deprecation': 0.16; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'oct': 0.16; 'quotes)': 0.16; 'str.format()': 0.16; 'string:': 0.16; 'switch.': 0.16; 'tuple,': 0.16; 'url:py3k': 0.16; '{0}': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'passes': 0.17; 'input': 0.18; 'otherwise,': 0.20; 'received:209.85.214.174': 0.21; 'statement': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'fine': 0.28; 'run': 0.28; 'facing': 0.29; 'prints': 0.29; 'types.': 0.29; 'points': 0.29; 'function': 0.30; 'point': 0.31; 'url:python': 0.32; 'switch': 0.32; 'print': 0.32; 'instead,': 0.33; 'to:addr:python- list': 0.33; 'code:': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'entered': 0.34; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'display': 0.36; 'does': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'further': 0.61; "you'll": 0.62; 'total': 0.65; 'want,': 0.65; 'food': 0.71; 'prompt': 0.78; '2.7.': 0.84; '270': 0.84; 'fat': 0.84; 'url:functions': 0.84; 'url:reference': 0.84; 'avoided.': 0.91
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=HF3LUT8tpLnUyXkL2oeW1BLGzWywqQzqLs8SMX2hlAI=; b=N16Je34wUN/5bHU+yJIVZ5Fe3rtxr8rBipfBAoQ1FZbYttcHrjPMwCF0Wd4cEF+T44 DogEhWqkuspXCt6DHBcRWXDtA7ZcD76lpgpvB7jCR1yp3sCmh6dRzo514ORES8elDD/P dELF/lWzs5eWbZY5D8E3zdMwTs/3RLMGA0zQvurtsuKIK8+Bb7LM7oHeEfI8Aelh7MWX gRe+eUR/LOXDBIcpAsI8wRq1TrjdhZwv5kYRY+xWVmFHyURHPg+TTEnKGEVqdymeFPBh P8243YBcXEMomPZ3y8mN/NDOZveKuZFZVUAyBGED28fcmi0mwshtDwU7dpfGqAwL7LDz kmyQ==
MIME-Version 1.0
In-Reply-To <CAJ2vgs4UwT_eEstMXB2GVG+bmkfS+-tAiqv04ASH26qTc8e2HQ@mail.gmail.com>
References <CAJ2vgs4UwT_eEstMXB2GVG+bmkfS+-tAiqv04ASH26qTc8e2HQ@mail.gmail.com>
Date Wed, 24 Oct 2012 21:52:21 +1100
Subject Re: Python input function not working in 2.7 version
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.2747.1351075943.27098.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351075943 news.xs4all.nl 6881 [2001:888:2000:d::a6]:54920
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32026

Show key headers only | View raw


On Wed, Oct 24, 2012 at 9:37 PM, Morten Engvoldsen <mortenengv@gmail.com> wrote:
> I am facing issue with input() of Python 2.7. When i run the program it
> doesn't display any line to take user input . Below is the code:
>
> But the above print function doesn't display the out put in same way. I am
> new to Python and i appreciate your help in advance.
>

Two points to note. Firstly, print is a statement in Python 2; you can
call on the print function with a future directive, but otherwise,
what you're doing there is passing a tuple to the print statement, so
it prints out like this:

('A food product having {0} fat grams and {1} total calories', 3, 270)


What you want, I think, is to explicitly use that as a format string:

print("A food product having {0} fat grams and {1} total
calories".format(fat_grams, total_calories))
A food product having 3 fat grams and 270 total calories

This then passes a single string to print, which works fine
(parentheses around a single thing do nothing; it's the comma that
makes the tuple, not the parens).

The second point to note is that, in Python 2, input() will *eval*
what the user types. Try your program and type in "1+2" (without the
quotes) at the prompt - you'll see that Python evaluates what you
typed, as though you'd entered "3". This is VERY DANGEROUS, and should
be avoided. Instead, use raw_input(), which does what you expect.

Alternatively, switch to Python 3, in which print is a function and
input() behaves exactly as you'd expect. But you'll still want to
explicitly call format().

Further reading:

http://docs.python.org/reference/simple_stmts.html#future-statements
http://docs.python.org/library/functions.html#input
http://docs.python.org/library/functions.html#raw_input
http://docs.python.org/library/stdtypes.html#str.format
http://docs.python.org/py3k/

Note, incidentally, that even though str.format() is called "new-style
formatting", there's no deprecation of "percent-style formatting"
(similar to C's sprintf function), so if you're familiar with that,
you needn't feel compelled to switch.

Hope that helps!

ChrisA

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


Thread

Re: Python input function not working in 2.7 version Chris Angelico <rosuav@gmail.com> - 2012-10-24 21:52 +1100

csiph-web