A goto-like usage of a function

B

Bart Nessux

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()
 
B

Bart Nessux

Bart said:
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()

Sorry... namespace pollution. Both the function
and a var within it were named "path_name"... I'm
an idiot... I need more coffee.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top