[Tutor] Program not accessing while loops

Glen Wheeler wheelege@tsn.cc
Tue, 1 May 2001 19:12:18 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0100_01C0D272.A3F1A040
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<snip snip snip>
    mainmen =3D raw_input(">")
    while mainmen =3D=3D 1:
<snip snip snip>

  Your getting a string here, and not an integer.  So, 'while mainmen =
=3D=3D 1' will never, ever be true.  Ever.
  So, what you want is to convert it to an integer...with something like =
:-

  mainmen =3D int(raw_input(">"))

  To account for something other than an integer, and to make sure it =
doesn't crash when the user writes something like 'go away!', wrap it in =
a try-except block.

  try:
      mainmen =3D int(raw_input(">"))
  except:
      print 'Please enter an integer'

  Hope that helped,
  Glen.

------=_NextPart_000_0100_01C0D272.A3F1A040
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>&lt;snip snip snip&gt;<BR><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;=20
mainmen =3D raw_input("&gt;")<BR>&nbsp;&nbsp;&nbsp; while mainmen =3D=3D =

1:</FONT><BR>&lt;snip snip snip&gt;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; Your getting a string here, and not an integer.&nbsp; So, =
'while=20
mainmen =3D=3D 1' will never, ever be true.&nbsp; Ever.</DIV>
<DIV>&nbsp; So, what you want is to convert it to an integer...with =
something=20
like :-</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; mainmen =3D int(raw_input("&gt;"))</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; To account for something other than an integer, and to make =
sure it=20
doesn't crash when the user writes something like 'go away!', wrap it in =
a=20
try-except block.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; try:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mainmen =3D =
int(raw_input("&gt;"))</DIV>
<DIV>&nbsp;&nbsp;except:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 'Please enter an =
integer'</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; Hope that helped,</DIV>
<DIV>&nbsp; Glen.</DIV></BODY></HTML>

------=_NextPart_000_0100_01C0D272.A3F1A040--