[Tutor] Loops and User Input
Marty Pitts
marty55@home.com
Mon, 5 Mar 2001 00:28:13 -0800
This is a multi-part message in MIME format.
------=_NextPart_000_001D_01C0A50B.2A3F7900
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
From: alan.gauld@bt.com=20
To: marty55@home.com=20
Sent: Sunday, March 04, 2001 9:16 AM
Subject: RE: [Tutor] Loops and User Input
HI, Glad to see you are working thru' the book. Your example looks =
fine=20
to me and the fact that you have elaborated on the version on the book =
is excellent. That's by far the best way to learn.
Just in case you haven't seen it there is an errata page at:
http://www.crosswinds.net/~agauld/book/errata.htm
And there are direct mail links to me there too.
Thanks, I appreciate the pointer.
But I can't see any immediate problems in your code other than the =
ones=20
Danny Yoo pointed out with strangeness using low multipliers.
However I couldn't help adding a few "thought provoking" comments :-)
multiplier =3D input("Which mulitplier value do you want to use? ")
for i in range(2, multiplier + 1):
for j in range(1, multiplier + 1):
=20
Are you sure you want this? If you use a multiplier of say 20 you =
will get
all of the tables from 2 to 20 times.
BUT they will also have all the elements from 1 to 20!
I suspect you want to make the second range be (1,13) to get the 1 =
to 12 values?
I noticed this. I am thinking about modifying the input for the =
user to be able to have choice for both variables.
Just a thought.
if i =3D=3D 2 and j =3D=3D 1:
print "---------------------------------- "
print "This table is the",i,"multiple table"
print "---------------------------------- "=20
So this only prints for the first time round since i is only 2 once.
Assuming you want the caption for every table I think you only=20
need to check for j=3D=3D1?=20
It is necessary to check for both values if the caption is to work =
correctly. If I check, as you suggest, only for equality on the variable =
j, double captions result for every table after the first one. In order =
for the heading to show up on only the first time around, a check is =
needed on both variables. I discovered this by trial and error.
=20
print "%d x %d =3D %d" % (j, i, j * i)
if i =3D=3D multiplier:
break
=20
Oooh! Now I never wrote about break :-)
Where did you find that trick?
Yours is not the only book on Python that I own. :-) However, I =
find it a good one for my pace.
But it will throw you out of the loop one iteration before I think =
you want to?
After all, you used multiplier+1 in the initial range. This will =
somewhat deeat the=20
purpose of that won't it?
Actually, it breaks at just the right spot. When the code runs, =
if I don't put a break here, it prints a caption at the end of the =
output that is useless, since the captions are for information at the =
start of each table.
print "---------------------------------- "
print "This table is the",i + 1 ,"multiple table"
print "---------------------------------- "
Using any input just returns to a prompt in 'idle'.
Don't understand why that is tho'...
Alan G.
Thanks for your critique. If you spot any errors in my reasonings =
above, please feel free to point them out.
Regards,
Marty.
------=_NextPart_000_001D_01C0A50B.2A3F7900
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4611.1300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dalan.gauld@bt.com=20
href=3D"mailto:alan.gauld@bt.com">alan.gauld@bt.com</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3Dmarty55@home.com =
href=3D"mailto:marty55@home.com">marty55@home.com</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Sunday, March 04, 2001 =
9:16=20
AM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> RE: [Tutor] Loops and =
User=20
Input</DIV>
<DIV><FONT face=3DArial size=3D2></FONT><BR></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>HI,=20
Glad to see you are working thru' the book. Your example looks fine=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>to=20
me and the fact that you have elaborated on the version on the book=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>is=20
excellent. That's by far the best way to learn.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT><FONT face=3DArial =
color=3D#0000ff=20
size=3D2><SPAN class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>Just=20
in case you haven't seen it there is an errata page =
at:</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001><A=20
=
href=3D"http://www.crosswinds.net/~agauld/book/errata.htm">http://www.cro=
sswinds.net/~agauld/book/errata.htm</A></SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>And=20
there are direct mail links to me there too.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001> Thanks, I appreciate the =
pointer.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT><FONT face=3DArial =
color=3D#0000ff=20
size=3D2><SPAN class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>But=20
I can't see any immediate problems in your code other than the ones=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>Danny Yoo pointed out with strangeness =
using low=20
multipliers.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>However I couldn't help adding a few =
"thought=20
provoking" comments :-)</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<BLOCKQUOTE=20
style=3D"PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px =
solid; MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial size=3D2>multiplier =3D input("Which =
mulitplier value do=20
you want to use? ")<BR>for i in range(2, multiplier +=20
1):<BR> for j in range(1, multiplier + =
1):<BR><SPAN=20
class=3D590055616-04032001><FONT=20
color=3D#0000ff> </FONT></SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>Are you sure you want this? If you use a =
multiplier=20
of say 20 you will get</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>all of the tables from 2 to 20=20
times.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>BUT they will also have all the elements =
from 1 to=20
20!</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN =
class=3D590055616-04032001>I=20
suspect you want to make the second range be (1,13) to get the 1 to =
12=20
values?</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001> I noticed this. =
I am=20
thinking about modifying the input for the user to be able to have =
choice=20
for both variables.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>Just a thought.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT><FONT face=3DArial =
size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN=20
=
class=3D590055616-04032001> </SPAN> &nb=
sp; =20
if i =3D=3D 2 and j =3D=3D 1:<BR> =
=20
print "----------------------------------=20
"<BR> print =
"This=20
table is the",i,"multiple table"<BR> =
=20
print "---------------------------------- "<SPAN=20
class=3D590055616-04032001><FONT=20
color=3D#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN=20
class=3D590055616-04032001></SPAN></FONT></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN =
class=3D590055616-04032001><FONT=20
color=3D#0000ff>So this only prints for the first time round =
since i is=20
only 2 once.</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN =
class=3D590055616-04032001><FONT=20
color=3D#0000ff>Assuming you want the caption for every table I =
think you only=20
</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN =
class=3D590055616-04032001><FONT=20
color=3D#0000ff>need to check for=20
j=3D=3D1?</FONT> </SPAN></FONT></FONT></DIV>
<BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001>It is necessary to check for both =
values if the=20
caption is to work correctly. If I check, as you suggest,=20
only for equality on the variable j, double captions =
result for=20
every table after the first one. In order for the heading to show =
up on=20
only the first time around, a check is needed on both variables. I =
discovered this by trial and =
error.</SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D590055616-04032001> </SPAN></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN=20
=
class=3D590055616-04032001> </SPAN> &nb=
sp; =20
print "%d x %d =3D %d" % (j, i, j * i)<BR> if i =
=3D=3D=20
multiplier:<BR> break<BR><SPAN=20
class=3D590055616-04032001><FONT=20
color=3D#0000ff> </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial color=3D#0000ff><SPAN=20
class=3D590055616-04032001>Oooh! Now I never wrote about break=20
:-)</SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial color=3D#0000ff><SPAN=20
class=3D590055616-04032001>Where did you find that=20
trick?</SPAN></FONT></FONT></DIV>
<BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001>Yours is not the only book on Python =
that I own.=20
:-) However, I find it a good one for my=20
pace.</SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><FONT size=3D2><FONT face=3DArial color=3D#0000ff><SPAN=20
class=3D590055616-04032001>But it will throw you out of the loop one =
iteration=20
before I think you want to?</SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial color=3D#0000ff><SPAN=20
class=3D590055616-04032001>After all, you used multiplier+1 in the =
initial=20
range. This will somewhat deeat the </SPAN></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial color=3D#0000ff><SPAN=20
class=3D590055616-04032001>purpose of that won't=20
it?</SPAN></FONT></FONT></DIV>
<BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001>Actually, it breaks at just the right =
spot. =20
When the code runs, if I don't put a break here, it prints a =
caption at=20
the end of the output that is useless, since the captions are for=20
information at the start of each =
table.</SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><FONT size=3D2><FONT face=3DArial><SPAN=20
class=3D590055616-04032001> </SPAN> print=20
"---------------------------------- "<BR> print =
"This=20
table is the",i + 1 ,"multiple table"<BR> print=20
"---------------------------------- "</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Using any input just returns to a =
prompt in=20
'idle'.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><SPAN class=3D590055616-04032001><FONT face=3DArial =
color=3D#0000ff=20
size=3D2>Don't understand why that is tho'...</FONT></SPAN></DIV>
<DIV><SPAN class=3D590055616-04032001></SPAN><FONT face=3DArial =
color=3D#0000ff=20
size=3D2><SPAN class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001>Alan G.</SPAN></FONT></DIV>
<BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><SPAN=20
class=3D590055616-04032001><FONT color=3D#800000>Thanks for your=20
critique.</FONT> <FONT color=3D#800000>If you spot any errors in =
my=20
reasonings above, please feel free to point them=20
out.</FONT></SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001>Regards,</SPAN></FONT></DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
class=3D590055616-04032001></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#800000 size=3D2><SPAN=20
=
class=3D590055616-04032001>Marty.</SPAN></FONT></DIV></BLOCKQUOTE></BLOCK=
QUOTE></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_001D_01C0A50B.2A3F7900--