I do not succeed in keeping a running total throughout a Python program. I declare a variable at the beginning of the module, and increment it in a subsequent function. I always get the error message:
UnboundLocalError: local variable 'urls_checked' referenced before assignment.
The only time when I do not get that error is when I declare the running total inside the function check_url
, but is nonsensical. Its value would each time reinitialize to 0.
Is it in this program not possible to keep a running total of the URLs checked?
Below I made a mockup very simplified version of the original program:
urls_checked = 0
def check_urls_in_page(html_page):
for var in range(4):
check_url(var)
def check_url(url):
# Check validity of url
# and increase the running total
# ...
urls_checked +=1
if __name__ == "__main__":
urls_checked = 0
html_page = "somepage.html"
check_urls_in_page(html_page)
print("Urls checked: ",urls_checked)