Python 3 defining a variable within a function
<p>I am working on my own code to encrypt/decrypt messages and all is working fine. Now I am just trying to tidy up the code a bit and I'm also trying to add error catching. I want to make this error catching within a function so I don't have to type out the error catching like 6 times within one block of code.</p>

<pre><code>c = 1
list = ['Y', 'N']
test1 = "H"

def f(test1):
    while c == 1:
            try:
                    test1 = raw_input("Input something yo")
                    if test1 not in list:
                            raise ValueError("Enter Y or N")
                    else:
                            return test1 
                            break
            except ValueError as error:
                    print (error)

a = f
a(test1)

a = test1
print (a)
if a == "Y":
    print ("Yes")
else:
    print ("No")
</code></pre>

<p>This is a test to practise doing this. However, I have not been able to successfully do it. So, in this code, I want to define a as either "Y" or "N" as a user input. I want a to call the function f and then test1 is the variable name. I want a to be test1 after the function has run. So if the function is running and the user types "Y", then "Yes" will be printed. If not, then "No" will be printed. For my actual script, I will need multiple values to be defined as the returned value from this function since I don't want to type out the error catching process so many times. If the user does not type "Y" or "N" then they have to type it again, so that part works. It's just returning the value test1 that I'm having trouble with.</p>

<p>At the moment, test1 is always "H" but if I don't have that line, I get this error:</p>

<pre><code>Traceback (most recent call last):
    File "FuncTest.py", line 19, in &lt;module&gt;
        a(test1)
NameError: name 'test1' is not defined
</code></pre>

<p>Any ideas how I can fix this? Thanks in advance everyone! :D</p>

<p>You are getting an error when you remove line 3 of your example as you are removing the definition of test1.</p>

<p>It isn't neccessary to pass values to a function in order to return them, a more general application of the same code is as follows:</p>

<pre><code>def wait_and_validate (validation_list):
    while True:
        the_input= raw_input("Input something yo")
        if the_input not in validation_list:
            raise ValueError("Input not In Validation List")
        return the_input
try:
    valid_input = wait_and_validate(['Y', 'N'])
except ValueError as error:
    print(error)
#do stuff with valid_input
</code></pre>

<p>You may wish to experiment with different return values rather than raising errors. Not sure what the official wisdom is, but from an ease of reuse perspective returning a tuple is probably easier to handle and saves defining your errors in your validation function (i.e. you might want to handle it differently depending on the part of the program you're in).</p>

python function