[Tutor] while with function

fleet@teachout.org fleet@teachout.org
Sat Nov 16 23:57:01 2002


Sigh.  If one enters a non-zero number first, the tries again WITHOUT
reinitializing y, it fails (because y no longer equals 0).  (Where's the
lid to my hole?!)

Thanks for the response.

				- fleet -

On Sat, 16 Nov 2002, Don Arnold wrote:

>
> ----- Original Message -----
> From: <fleet@teachout.org>
> To: "python tutor list" <tutor@python.org>
> Sent: Saturday, November 16, 2002 9:46 PM
> Subject: [Tutor] while with function
>
>
> > I've written a function getnum()
> >
> > def getnum()
> >    x=int(raw_input("get number: ")
> >    return x
> >
> > I'm trying to control a while loop as follows:
> >
> > y=0
> > while y==0:
> >    y=getnum()
> >    print y
> >
> > No matter what number I enter in response to getnum(), the while loop
> > prints y then quits.  My assumption is that if the number I enter in
> > response to getnum() is 0, y should be printed, and getnum() should be
> > invoked again.  Any number other than 0 should cause the loop to quit.
> >
> > Where am I going wrong?
> >
> > - fleet -
> >
>
> I'm not sure that you are. It works fine for me:
>
> >>> def getnum():
>     x=int(raw_input('get number: '))
>     return x
>
> >>> y = 0
> >>> while y == 0:
>     y = getnum()
>     print y
>
> get number: 0
> 0
> get number: 0
> 0
> get number: 4
> 4
> >>>
>
> Can you cut + paste your python session so we can see exactly what's going
> on?
>
> Don
>