[Tutor] Going Loopy over Loops
Billie
bjmartin98@pennswoods.net
Wed, 14 Aug 2002 18:30:54 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0024_01C243C0.B99C3840
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hello:
I'm using Win95 and Python version 2.2. I'm new to Python and =
programming.
I'm having trouble understanding how loops work, especially when they =
contain conditional statements. I have an exercise that I would like =
help with. By help I mean explain what's going on. I know what I'm =
asking is going to be time consuming but I'm kinda stuck here and you =
guys are my only source of programming help.
#Rewrite the area.py program to hae a separate function for=20
# the area of a square, the area of a rectangle, and the area
# of a circle. This program should include a menu interface.
print "The Area Calculator"
print "Please choose a letter for the option needed."
print
print
def print_options():
print "Options:"
print " 'p' Please choose an option."
print " 'c' Calculate the area of a Circle"
print " 'r' Calculate the area of a Rectangle"
print " 's' Calculate the area of a Square"
print " 'q' To quit the program"
def areaOfCircle(c_radius):
return 3.14*c_radius**2
def areaOfRectangle(length,width):
return length*width
def areaOfSquare(s_side):
return s_side**2
choice =3D"p"
while choice !=3D "q":
if choice =3D=3D "c":
radius =3D imput("Enter radius of circle: ")
while radius <=3D 0:
print "The number entered must be positive."
radius =3D imput("Enter radius of circle: ")
print "Area of circle is: ",areaOfCircle(radius)
elif choice =3D=3D "r":
l =3D=3Dinput("Enter length: ")
while l <=3D 0:
print "The number must be positive."
l =3D input("Enter length: ")
w =3D input("Enter width: ")
while w <=3D 0:
print "The number must be positive."
w =3D input("Enter width: ")
print "The area of a rectangle is: ",areaOfRectangle(l,w)
elif choice =3D "s":
side =3D input("Enter side length: ")
while side <=3D 0:
print "The number entered must be positive."
side =3D input("Enter side length: ")
print "The area of a square is: ",areaOfSquare(side)
elif choice !=3D "q":
print_options()
choice =3D raw_input("Option: ")
This program works as written. But beside knowing what the loops are =
doing I have a few questions about redundancy. There seems to be a lot =
of them in my program. Could I have made the positive number loop a =
function? Is it necessary, say for instance in "choice c", when I =
construct my while loop for the positive number to state again radius =
=3D input("Enter length:").
Anyone who would take the time to help me would be greatly appreciated.
bjmartin@pennswoods.net
=20
------=_NextPart_000_0024_01C243C0.B99C3840
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.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hello:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I'm using Win95 and Python version 2.2. =
I'm new to=20
Python and programming.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I'm having =
trouble understanding how=20
loops work, especially when they contain conditional statements. I =
have an=20
exercise that I would like help with. By help I mean explain what's =
going=20
on. I know what I'm asking is going to be time consuming but I'm =
kinda=20
stuck here and you guys are my only source of programming =
help.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>#Rewrite the area.py program to hae a =
separate=20
function for </FONT></DIV>
<DIV><FONT face=3DArial size=3D2># the area of a square, the area of a =
rectangle,=20
and the area</FONT></DIV>
<DIV><FONT face=3DArial size=3D2># of a circle. This program =
should include a=20
menu interface.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>print "The Area =
Calculator"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>print "Please choose a letter for the =
option=20
needed."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>print</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>print</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>def print_options():</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print =
"Options:"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print " 'p' Please =
choose an=20
option."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print " 'c' =
Calculate the area=20
of a Circle"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print " 'r' =
Calculate the=20
area of a Rectangle"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print " =
's' Calculate the=20
area of a Square"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> print " 'q' To quit =
the=20
program"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>def =
areaOfCircle(c_radius):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> return=20
3.14*c_radius**2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>def =
areaOfRectangle(length,width):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> return =
length*width</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>def areaOfSquare(s_side):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> return =
s_side**2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>choice =3D"p"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>while choice !=3D "q":</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> if choice =3D=3D =
"c":</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
radius =3D=20
imput("Enter radius of circle: ")</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> while=20
radius <=3D 0:</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; print=20
"The number entered must be positive."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
radius =3D imput("Enter radius of circle: =
")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
print "Area=20
of circle is: ",areaOfCircle(radius)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> elif choice =3D=3D =
"r":</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> l =
=3D=3Dinput("Enter length: ")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
while l <=3D=20
0:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
print "The number must be positive."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
l =3D input("Enter length: ")</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> w =3D=20
input("Enter width: ")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
while w <=3D=20
0:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
print "The number must be positive."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
w =3D input("Enter width: ")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
print "The=20
area of a rectangle is: ",areaOfRectangle(l,w)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> elif choice =3D =
"s":</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
side =3D=20
input("Enter side length: ")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
while side=20
<=3D 0:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
print "The number entered must be =
positive."</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
side =3D input("Enter side length: ")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =
print "The=20
area of a square is: ",areaOfSquare(side)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> elif choice !=3D =
"q":</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
print_options()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> choice =3D =
raw_input("Option:=20
")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>This program works as written. =
But beside=20
knowing what the loops are doing I have a few questions about =
redundancy. =20
</FONT><FONT face=3DArial size=3D2>There seems to be a lot of them in my =
program. Could I have made the positive number loop a =
function? Is=20
it necessary, say for instance in "choice c", when I construct my =
while=20
loop for the positive number to state again radius =3D input("Enter=20
length:").</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Anyone who would take the time to help =
me would be=20
greatly appreciated.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><A=20
href=3D"mailto:bjmartin@pennswoods.net">bjmartin@pennswoods.net</A></FONT=
></DIV>
<DIV><FONT face=3DArial =
size=3D2> =20
</FONT></DIV></BODY></HTML>
------=_NextPart_000_0024_01C243C0.B99C3840--