Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'works.': 0.09; 'subject:question': 0.10; 'martin': 0.11; "'))": 0.16; 'btw:': 0.16; 'indexerror:': 0.16; 'readable': 0.16; 'received:188.174': 0.16; 'received:80.91.229.3': 0.16; 'received :mnet-online.de': 0.16; 'received:plane.gmane.org': 0.16; 'subject:coding': 0.16; 'subject:format': 0.16; 'tuple': 0.16; '{0}': 0.16; '{1},': 0.16; 'index': 0.16; 'obviously': 0.18; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'skip:e 30': 0.24; 'text,': 0.24; 'text.': 0.24; 'question': 0.24; 'second': 0.26; 'post': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; "doesn't": 0.30; 'code': 0.31; 'about.': 0.31; 'font': 0.31; 'forces': 0.31; 'another': 0.32; 'third': 0.33; "can't": 0.35; 'but': 0.35; 'sequence': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'range': 0.61; 'first': 0.61; "you've": 0.63; 'sum': 0.64; 'more': 0.64; 'here': 0.66; 'between': 0.67; 'results': 0.69; 'calculations': 0.84; 'divided': 0.91; 'luck': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Sibylle Koczian Subject: Re: Newbie coding question - format error Date: Sun, 29 Jun 2014 16:09:42 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ppp-188-174-169-111.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 In-Reply-To: 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: 1404051003 news.xs4all.nl 2854 [2001:888:2000:d::a6]:46669 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73731 Am 29.06.2014 09:06, schrieb Martin S: > > x=int(input('Enter an integer ')) > y=int(input('Enter another integer ')) > z=int(input('Enter a third integer ')) > formatStr='Integer {0}, {1}, {2}, and the sum is {3}.' > equations=formatStr.format(x,y,z,x+y+z) > print(equations) > formatStr2='{0} divided by {1} is {2} with a reminder of {3}' > equations2=formatStr2.format(x,y,x//y,x%y) > print(equations2) > > And obviously this works. > But the question is: if I want to keep the results of {2} and {3} > between the first instance (formatStr) and the second (formatStr2) how > would I go about it? Apprently using {4} and {5} instead results in a > index sequence error as in > > IndexError: tuple index out of range > {0} ... {3} are just placeholders in your format strings, they can't exist outside of them. And you can't put more placeholders into the format string than you've got values to put into them. That's what the IndexError is about. But nothing forces you to put your calculations into the call to format(). Instead, give names to the results you want to keep: mysum = x + y + z equation_sentence_1 = formatStr.format(x, y, z, mysum) ... myquotient = x // y myremainder = x % y # or, nicer: (myquotient, myremainder) = divmod(x, y) equation_sentence_2 = formatStr2.format(x, y, myquotient, myremainder) ... Now you still can do all you want with mysum, myquotient, myremainder. HTH Sibylle BTW: it's better to post here using text, not HTML. Not all newsreaders and mail clients used for this list cope well with HTML. And it's just luck that your code doesn't contain indentations, they might not survive. Moreover code is much more readable with a fixed font which you get as a by-product using text.