Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'none,': 0.05; 'correct.': 0.07; 'exception.': 0.07; 'raises': 0.07; 'python': 0.09; 'brackets': 0.09; 'function:': 0.09; 'none.': 0.09; 'received:192.168.1.101': 0.09; '"none': 0.16; '%s,': 0.16; '(name,': 0.16; 'carefully.': 0.16; 'quest': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'string,': 0.17; 'tim': 0.18; '>>>': 0.18; 'example': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'wrote': 0.26; 'first,': 0.27; 'lines': 0.28; '-0700,': 0.29; "d'aprano": 0.29; 'statements': 0.29; 'steven': 0.29; 'no,': 0.29; 'fri,': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'operate': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'clear': 0.35; 'so,': 0.35; 'subject:?': 0.35; 'add': 0.36; 'thank': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'first': 0.61; 'between': 0.63; 'color': 0.69; '2013': 0.84; 'all!': 0.84; 'received:212.52.84': 0.84; 'received:libero.it': 0.84; 'received:212.52': 0.91 X-CTCH-Spam: Unknown X-CTCH-RefID: str=0001.0A0C0205.514DE0D4.00EF,ss=1,re=0.000,fgs=0 X-libjamoibt: 1823 Date: Sat, 23 Mar 2013 18:05:20 +0100 From: leonardo User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130307 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: how does the % work? References: <7qbqk8p64dp4v2qvgfogvigi91gjdt5k3r@4ax.com> <514d5bef$0$30001$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <514d5bef$0$30001$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364058458 news.xs4all.nl 6915 [2001:888:2000:d::a6]:37211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41747 thank you all! Il 23/03/2013 8.38, Steven D'Aprano ha scritto: > On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote: > >> leonardo selmi wrote: >>> i wrote this example : >>> >>> name = raw_input("What is your name?") >>> quest = raw_input("What is your quest?") >>> color = raw_input("What is your favorite color?") >>> >>> print """Ah, so your name is %s, your quest is %s, and your favorite >>> color is %s.""" % (name, quest, color) >> No, you didn't. You wrote: >> >> print('''Ah, so your name is %s, your quest is %s, and your >> favorite color is %s.''') % (name, quest, color) > > The difference between those two statements may not be entirely clear to > someone not experienced in reading code carefully. > > Consider the difference between: > > print(a % b) > > print(a) % b > > In the first example, the round brackets group the "a % b", which is > calculated first, then printed. > > In the second example, in Python 3, the "print(a)" is called first, which > returns None, and then "None % b" is calculated, which raises an > exception. > > Just to add confusion, the two lines are exactly the same in Python 2, > where Print is not a function! > > >> You are using Python 3. In Python 3, "print" is a function that returns >> None. So, the error is exactly correct. To fix it, you need to have >> the % operator operate on the string, not on the result of the "print" >> function: >> >> print('''Ah, so your name is %s, your quest is %s, and your >> favorite color is %s.''' % (name, quest, color)) > Exactly correct. > > >