0

I am trying to figure out how to update empty variables outside of a function in Python. The reason for this is to create a loop that queries a user about additional parameters they would be able to add.

What I have so far looks like:

param1 = ""
param 2 = ""
typeans = ['parameter 1', 'param 2']
typeansa = ['parameter 2', 'param 2']

def addparams():
    while True:
        again = raw_input('Would you like to add another parameter? Enter y/n: ')
        if again == "n":
            break
        if again == "y":
        additionalparams = raw_input("Would you like to add any other parameters? (Parameter 1, Parameter 2): ")
            if additionalparams.lower() in typeans:
                param1 = raw_input('Please enter the first search criteria: ')
                param1.update() = param1+"one"
            elif additionalparams.lower() in typeansa:
                param2 = raw_input('Please enter the second search criteria: ')
                param2.update() = param2+"two"
            addparams()
addparams()
url = www.website.com/+param1+param2

The purpose of this code would be to allow me to query the user about if they had additional parameters and then add them to a url/uri. The reason I left them blank outside of the fuction is in case the user only opted to use one of the variables or a few of them (in my real code I have 7 parameters).

I understand this may not be the best way to go about this problem and would greatly appreciate any suggestions. I am new to Python and would welcome any help.

Thanks!

J Brewer
  • 35
  • 3

2 Answers2

0

You should take a look at the global statement for a direct answer to your question.

def addparams():
    global param1
    global param2
    ...

Besides that, I must say that your code has also a weird indentation and a few typos: param 2 is not a valid name in Python, the second item of typeans should probably be 'param 1' and you have an additional call to addparams() in your loop. Finally, I don't know what param1.update() is. It should rather look like:

param1 = ""
param2 = ""
def addparams():
    global param1
    global param2
    typeans = ['parameter 1', 'param 1']
    typeansa = ['parameter 2', 'param 2']
    while True:
        again = raw_input('Would you like to add another parameter? Enter y/n: ')
        if again == "n":
            break
        if again == "y":
            additionalparams = raw_input("Would you like to add any other parameters? (Parameter 1, Parameter 2): ")
            if additionalparams.lower() in typeans:
                param1 = raw_input('Please enter the first search criteria: ')
                param1 = param1+"one"
            elif additionalparams.lower() in typeansa:
                param2 = raw_input('Please enter the second search criteria: ')
                param2 = param2+"two"

However, it's not necessarily the best way; I'd rather have returned the list of params from the function:

def addparams():
    param1 = ''
    param2 = ''
    typeans = ['parameter 1', 'param 1']
    typeansa = ['parameter 2', 'param 2']
    while True:
        [...] # same code as above
    return param1, param2

param1, param2 = addparams()
url = 'www.website.com/'+param1+param2 # not sure that's the url you want

You might want to go further and actually return the full url:

def createURL():
    param1 = ''
    param2 = ''
    typeans = ['parameter 1', 'param 1']
    typeansa = ['parameter 2', 'param 2']
    while True:
        again = raw_input('Would you like to add another parameter? Enter y/n: ')
        if again == "n":
            break
        if again == "y":
            additionalparams = raw_input("Would you like to add any other parameters? (Parameter 1, Parameter 2): ")
            if additionalparams.lower() in typeans:
                param1 = raw_input('Please enter the first search criteria: ')
                param1 = param1+"one"
            elif additionalparams.lower() in typeansa:
                param2 = raw_input('Please enter the second search criteria: ')
                param2 = param2+"two"
    return 'www.website.com/'+param1+param2

url = createURL()
Francis Colas
  • 3,459
  • 2
  • 26
  • 31
  • 3
    While technically correct, teaching new programmers to solve everything with `global` is not really a good idea. – Lee White Mar 31 '15 at 19:48
  • Nobody here is claiming `global` should be the solution to everything but it is the direct solution to OP's issue (whereas the comments about `*args` and `**kwargs` are out of scope). Moreover I've added more info/fixes and alternative solutions. – Francis Colas Mar 31 '15 at 20:02
  • Rather than using global I moved the parameters inside the function and that solved my issue, I appreciate the help from everybody! – J Brewer Apr 01 '15 at 14:34
0

Without a bigger frame of reference, it may sound totally tangential, but you could consider simply building the entire destination in one function and rid yourself of the entire problem of updating outside variables.

def get_params():
    target = 'http://www.example.com'
    asking = True
    while asking:
        next_param = raw_input('Add another param? Y/N')
        if 'y' in next_param.lower():
            target += "/{}".format(raw_input('Enter parameter: '))
        else:
            asking = False
    return target

If you've got some bigger need to store each parameter separately, then you could have it just return a list of your params and then use '/'.join(x for x in returned_list). This would have the added benefit of giving you both the list of parameters as well as the final search string, again without having to have all these global variables floating around.

def get_list_of_params():
    target = ['http://www.example.com']
    asking = True
    while asking:
        next_param = raw_input('Add another param? Y/N')
        if 'y' in next_param.lower():
            target.append(raw_input('Enter parameter: '))
        else:
            asking = False
    return target

Which you would use as follows:

param_list = get_list_of_params()
search_string = '/'.join(param_list)

In this way, instead of setting up global variables to alter elsewhere, you're just working with return values that are already ready to be used.