[Tutor] while with function

Rick Pasotto rickp@telocity.com
Sun Nov 17 15:33:02 2002


On Sun, Nov 17, 2002 at 03:09:54PM -0500, fleet@teachout.org wrote:
> On Sun, 17 Nov 2002, Danny Yoo wrote:
> >
> > By the way, here's another way to write your loop:
> >
> > ###
> > while 1:
> >     y = getnum()
> >     print y
> >     if y: break
> > ###
> 
> As I read this it means: if y is any number, exit the loop?

No. It means "if y is true" which means "if y != 0".

> How would that differ (in results) from y=getnum() without the loop?
> 
> I like the concept of "while 1:" but I've had problems getting it to break
> properly.  I suspect I'm having "scope" problems.
> 
> The "real" problem is a list of variable that I want to display (preceded
> by numbers) possibly repeatedly for correction (in necessary).
> 
> 1. Surname=whatever
> 2. Given Name=whatever
> 3. Nickname=whatever
> 
> Enter variable number to make correction or "zero" to quit.
> 
> Assuming the above text is in a function called get_list(), then
> 
> while 1:
>    opt=getnum()
>    if opt==1:
>       s_name=raw_input("Surname: ")
>       get_list()
>    elif opt==2:
>       g_name=raw_input("Given Name: ")
>       get_list()
>    elif opt==3:
>       n_name="raw_input("Nickname: ")
>       get_list()
>    elif opt==0:
>       break
> 
> Something like this, right?

Why not:

def get_name(prompt):
	return raw_input(prompt)

while 1:
	opt = getnum()
	if opt == 1:
		s_name = get_name("Surname: ")
	elif opt == 2:
		g_name = get_name("Given Name: ")
	elif opt == 3:
		n_name = get_name("Nickname: ")
	else:
		break

Note that this will break out of the while loop if any number other than
1, 2, or 3 is entered. That may not be what you want.

-- 
"A promiscuous person is someone who is getting more sex than you are."
		-- Victor Lownes
    Rick Pasotto    rickp@telocity.com    http://www.niof.net