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


Groups > comp.lang.python > #73724

Newbie coding question - format error

Date 2014-06-29 09:06 +0200
Subject Newbie coding question - format error
From Martin S <shieldfire@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11321.1404025599.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

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


Thread

Newbie coding question - format error Martin S <shieldfire@gmail.com> - 2014-06-29 09:06 +0200

csiph-web