Re: Newbie coding question - format error
Thanks for the input. The main thing was that the replacement fields were only valid for their local environment. /martin On 29 Jun 2014, Terry Reedy wrote: >On 6/29/2014 3:06 AM, Martin S wrote: > >A couple of additional notes: > >> 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}.' > >When the replacement fields and arguments are in the same order, the >indexes are optional. The following works, and might be less confusing. > >formatStr = 'Integer {}, {}, {}, and the sum is {}.' > >> equations=formatStr.format(x,y,z,x+y+z) > >We no longer have a space shortage ;-). The following is easier to >read. > >equations = formatStr.format(x, y, z, x+y+z) > >> print(equations) > >Compute quotient and remainder with the divmod function. > >q, r = divmod(x, y) > >Both are computed at once and x // y and x % y just toss the other >answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1] -- Sent with K-@ Mail - the evolution of emailing.-- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie coding question - format error
Thanks for the input. The main thing was that On 29 Jun 2014, Terry Reedy wrote: >On 6/29/2014 3:06 AM, Martin S wrote: > >A couple of additional notes: > >> 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}.' > >When the replacement fields and arguments are in the same order, the >indexes are optional. The following works, and might be less confusing. > >formatStr = 'Integer {}, {}, {}, and the sum is {}.' > >> equations=formatStr.format(x,y,z,x+y+z) > >We no longer have a space shortage ;-). The following is easier to >read. > >equations = formatStr.format(x, y, z, x+y+z) > >> print(equations) > >Compute quotient and remainder with the divmod function. > >q, r = divmod(x, y) > >Both are computed at once and x // y and x % y just toss the other >answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1] -- Sent with K-@ Mail - the evolution of emailing.-- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie coding question - format error
On 6/29/2014 3:06 AM, Martin S wrote: A couple of additional notes: 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}.' When the replacement fields and arguments are in the same order, the indexes are optional. The following works, and might be less confusing. formatStr = 'Integer {}, {}, {}, and the sum is {}.' equations=formatStr.format(x,y,z,x+y+z) We no longer have a space shortage ;-). The following is easier to read. equations = formatStr.format(x, y, z, x+y+z) print(equations) Compute quotient and remainder with the divmod function. q, r = divmod(x, y) Both are computed at once and x // y and x % y just toss the other answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1] -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie coding question - format error
In article , Sibylle Koczian wrote: > Am 29.06.2014 09:06, schrieb Martin S: > > 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. This seems like a confusing error message to me. I've opened http://bugs.python.org/issue21879 on this. -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie coding question - format error
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. -- https://mail.python.org/mailman/listinfo/python-list