Newbie coding question - format error

Martin S shieldfire at gmail.com
Sun Jun 29 03:06:29 EDT 2014


Hi again,

Still working leisurely through the tutorial here:
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html

An excise at the end of second 1.10 says to
Write a version of the quotient problem in *Exercise for Quotients*
<http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html#quotientproblem>,
quotientformat.py, that uses the string format method to construct the same
final string as before. Again be sure to give a full sentence stating both
the integer quotient and the remainder.


So I did

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

/Martin S
--
Regards,

Martin S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140629/f234b67c/attachment.html>


More information about the Python-list mailing list