A goto-like usage of a function

Bart Nessux bart_nessux at hotmail.com
Thu Jul 29 11:55:03 EDT 2004


Tim Golden wrote:
> [Bart Nessux]
> 
> | > Here's the function... if the user wants to re-enter the 
> | path name... 
> | > I'm simply calling the function again... is this wrong???
> 
> [... snip function ...]
> 
> | Sorry... namespace pollution. Both the function 
> | and a var within it were named "path_name"... I'm 
> | an idiot... I need more coffee.
> 
> Yes. That was the most immediate problem. But the other
> one is (only slightly) more subtle: you're going round
> again by having the function call itself. Now, while this
> will in fact work (it's called recursion, in case you've
> not come across the concept) it's not the best way to do
> this. All you need is a loop of this sort:
> 
> <code>
> 
> while 1:
>   chosen_path = raw_input ("What is the path?")
>   is_this_correct = \
>     raw_input ("You chose: %s. Is this correct?" % chosen_path)
>   
>   if is_this_correct == 'Y':
>     break
>   elif is_this_correct == 'X':
>     sys.exit ()
>   elif is_this_correct == 'N':
>     continue
>   else:
>     print "I'm sorry; I don't understand"
> 
> </code>
> 
> Obviously, there are all sorts of variations on
> this theme, but this will normally be a more
> natural fit for this kind of operation.
> 
> TJG

I understand recursion to be a loop or a loop to 
be recursion... however you prefer to look at it. 
Whether it's a function calling itself until the 
user gets the input right or a while statement w/i 
a function waiting for correct input... IMO, it's 
the same thing. With that said, there may be 
performance issues that I'm unaware of that make 
your approach better or worse  than mine... 
outside that, I think either approach is worthwhile.




More information about the Python-list mailing list