How to call a function correctly
<p>Here is the function which has four parameters. </p>

<pre><code>import urllib
import xlrd
from xlwt import Workbook

def getClose(q, i ,p, f):
    inputQ=q;
    inputI=str(i);
    inputP=str(p);
    inputF=f;

    address='http://www.google.com/finance/getprices?q='+inputQ+'&amp;i='+inputI+'&amp;p='+inputP+'d&amp;f='+inputF;
    response = urllib.request.urlopen(address);
    data = response.readlines();
    newData = data[7:len(data)];
    result=[];
    amount =0;
    for line in newData:
        content =str((line.decode("utf-8", errors='ignore').split("\n")[0]));
        result.insert(amount,content);
        amount = amount +1;
        #write the list of result into the excel file
    writeBook= Workbook();
    writeSheet= writeBook.add_sheet('Close Price');
    for n in range(amount):
        writeSheet.write(n,0,result[n]);
    writeBook.save('close price.xls');
</code></pre>

<p>But now I want to get the parameter q from a excel,what should I do? 
I know how to write it by using three parameters.</p>

<pre><code>import urllib
import xlrd
from xlwt import Workbook

def getClose(i ,p, f):
    workbook = xlrd.open_workbook('test1.xlsx');
    sheet=workbook.sheet_by_index(0);

    num_rows = sheet.nrows -1;

    inputI=str(i);
    inputP=str(p);
    inputF=f;

    writeBook= Workbook();
    writeSheet= writeBook.add_sheet('Close Price');
    for i in range(0,num_rows+1):
        inputS=sheet.cell_value(i,0);
        address='http://www.google.com/finance/getprices?q='+inputS+'&amp;i='+inputI+'&amp;p='+inputP+'d&amp;f='+inputF;
        response = urllib.request.urlopen(address);
        data = response.readlines();
        newData = data[7:len(data)];
        result=[];
        amount =0;
        for line in newData:
            content =str((line.decode("utf-8", errors='ignore').split("\n")[0]));
            result.insert(amount,content);
            amount = amount +1;
        for n in range(amount):
            writeSheet.write(n,i,result[n]);
    writeBook.save('close price.xls');
</code></pre>

<p>What should I do here? Really appreciate it.</p>

<p>Doing too many things in a single function is not advisable. Looking at the function, there are three main operations:</p>

<ol>
<li>from a spreadsheet (test1.xlsx) get a query parameter (q)  retrieve</li>
<li>and parse values from a webpage save the parsed results in a</li>
<li>spreadsheet (close price.xls)</li>
</ol>

<p>What I would suggest is to make a function <code>getClose</code> only for #2, and then use it inside a main loop that will read parameter <code>q</code> from <code>test1.xlsx</code> and write the results in <code>close price.xls</code>. Pseudo code like:</p>

<pre><code># THIS IS PSEUDO-CODE, not python...
open(input_spreadsheet)
open(output_spreadsheet)
for row in input_spreadsheet:
    q = parse_row(row)
    result = getClose(&lt;adjust parameters after function rewrite&gt;)
    write_row(output_spreadsheet)
</code></pre>

python python-3.x