2
html += '''
<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    '''
def get_bus_metrics (met,name):
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''

get_bus_metrics (g1,'R')

UnboundLocalError: local variable 'html' referenced before assignment

I am getting this error. Can someone please suggest me what I am missing here, why I am getting the above error.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 3
    Do you mean to use += on the first line? If html isn't already assigned and you try to add to it that's where your error's coming from – strava Oct 18 '18 at 05:34
  • You are creating crioss-workable code for python 2.7 and 3.6? If not, please only tag the version you use. – Patrick Artner Oct 18 '18 at 05:54
  • @strava Html needs a `` and `` tag before the `` tag makes sense. Line 1 is probably a copy&paste problem. The problem is far more likely to be that the code uses `html` in global scope and tries to modify it in the `get_bus_metric()` function without declaring it global or providing it as parameter - there the += hits as well
    – Patrick Artner Oct 18 '18 at 06:04
  • @PatrickArtner I just the responded to your code, could you please take a look. I am not able to output of the function to the html code – Eric Dsilva Oct 18 '18 at 08:18

2 Answers2

3

Fix the += if the variable was not used before and provide it to the function:

# fix here - no += unless you decleared html as some string beforehand
html = '''
<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    '''
# fix here - else not known
def get_bus_metrics (met,name,html):
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''
    return html

html = get_bus_metrics (range(1,5),'R',html)  # provide as parameter: cleaner

print(html) # 

Output:

<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    <th>1</th><th>2</th><th>3</th><th>4</th></tr><tr><th>R</th>

or (less preferable) declare it global:

def get_bus_metrics (met,name,html):
    # don't use globals, they are _evil_ - if you need to _modify_ smth
    # from global scope, you need to announce this in the function
    # reading works without if decleard earlier then the function, 
    # changing it needs this line:
    global html   
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''

Tip 1:
Better string formatting using str.format() or f-strings / PEP-0498 / Literal String Interpolation

Tip 2:
Adding to strings in a loop is wasteful - it constructs lots of intermediate strings that are thrown away. Use a list instead

def get_bus_metrics (met,name,html):
    t = []
    for i in met:
        t.append('<th>{}</th>'.format(i))  # using format(..)
    t.append(f'</tr><tr><th>{name}</th>')  # using string interpol
    return html+''.join(t)                 # faster, less wasted intermediate strings

Doku:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • def get_bus_metrics(value,name,html_table,sym=None): for i in value: lol.append('{:,.0f}'.format(i)) html_table += ''''''+name+'''''' for i in lol: if sym is not None: html_table += '' + '$' + str(i) + '' else: html_table += '' + ' ' + str(i) + '' html_table += '''''' get_bus_metrics(g2,'Row',html_table,1) Hey Patrick any thought what I am missing here, Row is not adding in the HTML code – Eric Dsilva Oct 18 '18 at 08:09
  • @EricDsilva Comments are unsuitable for code. Your function uses `lol` without defining it first - try `lol = []` at top. The whole indentation is missing in comments. You do not return the constructed string from your function. If you have antoher problem, post a second question if it is not realted - else edit and add to this question. Do not ask different things in one question, because it makes the reusability of answers worse (i.e. asking for apples, getting apples answer, asking for pumpkins again - ppl searching after pumpkins getting lots of apples answers - kind of thing). – Patrick Artner Oct 18 '18 at 11:51
0

html+= someVal is same as html = html + someVal.

Having not been initialized before the variable html is undefined.

Since html is undefined you can't perform html + someVal