A goto-like usage of a function

Bart Nessux bart_nessux at hotmail.com
Thu Jul 29 17:17:19 CEST 2004


Here's a function that I want to use to capture 
user input. They enter a path and then confirm 
that it's the correct path or not or decide to 
quit the program if they want. The 'y' (the path 
is right, let's continue) and the 'q' (I hate your 
stupid program and want to stop it now) both work 
fine. However the 'n' (I've entered the path 
incorrectly and want to do it again produces this 
error:

Traceback (most recent call last):
   File "clean_file_names_function_better.py", 
line 16, in ?
     path_name()
   File "clean_file_names_function_better.py", 
line 14, in path_name
     path_name()
TypeError: 'str' object is not callable

Here's the function... if the user wants to 
re-enter the path name... I'm simply calling the 
function again... is this wrong???

def path_name():
    import sys
    path_name = raw_input("\nEnter the absolute 
path to the directory that contains the files... 
for example, /home/user: ")
    print "\nYou entered: ", path_name, "\n"
    confirm  = raw_input("Is this the correct 
path: (press 'y' for YES 'n' for NO or 'q' to 
QUIT)\n\n")
    if confirm == 'y':
       print "\nStarting Program.\n"
       return path_name
    elif confirm == 'q':
       print '\n'
       sys.exit()
    else:
       print "\nTry again.\n"
       path_name()

path_name()



More information about the Python-list mailing list