A goto-like usage of a function

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Jul 29 11:30:09 EDT 2004


[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


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list