[Tutor] while with function
Don Arnold
darnold02@sprynet.com
Sat Nov 16 23:06:01 2002
----- 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