[Tutor] Loops and User Input
Marty Pitts
marty55@home.com
Fri, 2 Mar 2001 11:52:27 -0800
This is a multi-part message in MIME format.
------=_NextPart_000_001D_01C0A30F.410A39C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I'm a python newbie and I have been going through Mr. Gauld's book and =
have a question.
Following an exercise to understand loops, the following came about:
for i in range(2, 13):
for j in range(1, 13):
if i =3D=3D 2 and j =3D=3D 1:
print "---------------------------------- "
print "This table is the",i,"multiple table"
print "---------------------------------- "
print "%d x %d =3D %d" % (j, i, j * i)
if i =3D=3D 12:
break
print "---------------------------------- "
print "This table is the",i + 1 ,"multiple table"
print "---------------------------------- "
Which works fine.
However, when I add user input to let the range be dependant on this =
input like this:
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):
if i =3D=3D 2 and j =3D=3D 1:
print "---------------------------------- "
print "This table is the",i,"multiple table"
print "---------------------------------- "
print "%d x %d =3D %d" % (j, i, j * i)
if i =3D=3D multiplier:
break
print "---------------------------------- "
print "This table is the",i + 1 ,"multiple table"
print "---------------------------------- "
Using any input just returns to a prompt in 'idle'.
Any help will be appreciated.
Thanks,
------=_NextPart_000_001D_01C0A30F.410A39C0
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>
<DIV><FONT face=3DArial size=3D2>I'm a python newbie and I =
have been going=20
through Mr. Gauld's book and have a question.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Following an exercise to understand =
loops, the=20
following came about:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>for i in range(2, =
13):<BR> for j=20
in range(1, 13):<BR> if i =3D=3D 2 =
and j =3D=3D=20
1:<BR> print=20
"---------------------------------- "<BR> =
=20
print "This table is the",i,"multiple=20
table"<BR> print =
"---------------------------------- "<BR> =
=20
print "%d x %d =3D %d" % (j, i, j * i)<BR> if i =3D=3D =
12:<BR> break<BR> =
print=20
"---------------------------------- "<BR> print "This =
table is=20
the",i + 1 ,"multiple table"<BR> print=20
"---------------------------------- "</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Which works fine.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>However, when I add user input to let =
the range be=20
dependant on this input like this:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>multiplier =3D input("Which mulitplier =
value do you=20
want to use? ")<BR>for i in range(2, multiplier + =
1):<BR> for=20
j in range(1, multiplier + 1):<BR> =
if i =3D=3D=20
2 and j =3D=3D 1:<BR> =
print=20
"---------------------------------- "<BR> =
=20
print "This table is the",i,"multiple=20
table"<BR> print =
"---------------------------------- "<BR> =
=20
print "%d x %d =3D %d" % (j, i, j * i)<BR> if i =3D=3D =
multiplier:<BR> =
break<BR> =20
print "---------------------------------- "<BR> print =
"This=20
table is the",i + 1 ,"multiple table"<BR> print=20
"---------------------------------- "</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><FONT face=3DArial size=3D2>Any help will be =
appreciated.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV></BODY></HTML>
------=_NextPart_000_001D_01C0A30F.410A39C0--