python - Updating empty variables outside a function -


i trying figure out how update empty variables outside of function in python. reason create loop queries user additional parameters able add.

what have far looks like:

param1 = "" param 2 = "" typeans = ['parameter 1', 'param 2'] typeansa = ['parameter 2', 'param 2']  def addparams():     while true:         again = raw_input('would add parameter? enter y/n: ')         if again == "n":             break         if again == "y":         additionalparams = raw_input("would add other parameters? (parameter 1, parameter 2): ")             if additionalparams.lower() in typeans:                 param1 = raw_input('please enter first search criteria: ')                 param1.update() = param1+"one"             elif additionalparams.lower() in typeansa:                 param2 = raw_input('please enter second search criteria: ')                 param2.update() = param2+"two"             addparams() addparams() url = www.website.com/+param1+param2 

the purpose of code allow me query user if had additional parameters , add them url/uri. reason left them blank outside of fuction in case user opted use 1 of variables or few of them (in real code have 7 parameters).

i understand may not best way go problem , appreciate suggestions. new python , welcome help.

thanks!

you should take @ global statement direct answer question.

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

besides that, must code has weird indentation , few typos: param 2 not valid name in python, second item of typeans should 'param 1' , have additional call addparams() in loop. finally, don't know param1.update() is. should rather 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 add parameter? enter y/n: ')         if again == "n":             break         if again == "y":             additionalparams = raw_input("would add other parameters? (parameter 1, parameter 2): ")             if additionalparams.lower() in typeans:                 param1 = raw_input('please enter first search criteria: ')                 param1 = param1+"one"             elif additionalparams.lower() in typeansa:                 param2 = raw_input('please enter second search criteria: ')                 param2 = param2+"two" 

however, it's not best way; i'd rather have returned list of params function:

def addparams():     param1 = ''     param2 = ''     typeans = ['parameter 1', 'param 1']     typeansa = ['parameter 2', 'param 2']     while true:         [...] # same code above     return param1, param2  param1, param2 = addparams() url = 'www.website.com/'+param1+param2 # not sure that's url want 

you might want go further , return full url:

def createurl():     param1 = ''     param2 = ''     typeans = ['parameter 1', 'param 1']     typeansa = ['parameter 2', 'param 2']     while true:         again = raw_input('would add parameter? enter y/n: ')         if again == "n":             break         if again == "y":             additionalparams = raw_input("would add other parameters? (parameter 1, parameter 2): ")             if additionalparams.lower() in typeans:                 param1 = raw_input('please enter first search criteria: ')                 param1 = param1+"one"             elif additionalparams.lower() in typeansa:                 param2 = raw_input('please enter second search criteria: ')                 param2 = param2+"two"     return 'www.website.com/'+param1+param2  url = createurl() 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -