-2

Everything works perfect for a couple hours an then i'll get this error, an it will stop running.

todolist_items = len(todoalso)
UnboundLocalError: local variable 'todoalso' referenced before assignment

I think this is the section Im having trouble with but I dont understand why.

    response = requests.get("https://beta.todoist.com/API/v8/tasks", params={"token":todoist_TOKEN})
if response.status_code == 200:
    todoalso = response.json()
global todolist_items
todolist_items = len(todoalso)
john smith
  • 29
  • 1
  • 4
  • 2
    Looks like you are not getting `200` response from your api call – Rakesh May 28 '18 at 06:51
  • bcos the response status_code is not 200,define todoalso to None – Smart Manoj May 28 '18 at 06:51
  • Just ref the todoalso at the begining ... = = – MT-FreeHK May 28 '18 at 06:51
  • There are hundreds of "duplicate" questions for this error. It's as simple as a variable only being assigned a value if a condition evaluates to true, and not being initialised beforehand, resulting in the variable not existing in the case where the `if` statement evaluates to false. – rst-2cv May 28 '18 at 06:53

3 Answers3

2

You need to capture cases where the response fails, and log it to see why.

if response.status_code == 200:
    todoalso = response.json()
else:
    todoalso = None
    print response.status_code,response
Shivam Singh
  • 1,584
  • 1
  • 10
  • 9
  • This will fix the error described by OP, but it means they will never find out what the actual problem is – rst-2cv May 28 '18 at 07:01
  • Thanks, added a print statement to find out the actual problem. Rest of the debugging is upto OP's discretion. – Shivam Singh May 28 '18 at 07:08
0

I'll go one step further than the others and suggest this:

response = requests.get("https://beta.todoist.com/API/v8/tasks", params={"token":todoist_TOKEN})
global todolist_items

if response.status_code == 200:
    todoalso = response.json()
    # Let's assign this variable here, where we know that the status code is 200
    todolist_items = len(todoalso)
else:
    # instead of simply assigning the value "None" to todoalso, let's return the response code if it's not 200, because an error probably occurred
    print response.status_code
rst-2cv
  • 1,130
  • 1
  • 15
  • 31
-1

In this case I would think that the response code is not 200, so it directly goes to

todolist_items = len(todoalso)

without the assignment in the if branch. Maybe it would be better to modify it as

    response = requests.get("https://beta.todoist.com/API/v8/tasks", params={"token":todoist_TOKEN})
if response.status_code == 200:
    todoalso = response.json()
    global todolist_items
    todolist_items = len(todoalso)

As suggested by @ResetACK, I added a little change

    response = requests.get("https://beta.todoist.com/API/v8/tasks", params={"token":todoist_TOKEN})
if response.status_code == 200:
    todoalso = response.json()
    global todolist_items
    todolist_items = len(todoalso)
else:
    requests.raise_for_status()
  • This will stop the error described by OP from occurring, however it exposes other problems, such as, what happens if `response.status_code` doesn't equal `200`? – rst-2cv May 28 '18 at 07:00
  • @ResetACK, thanks for suggestions. I have modified my answer – Yan Linxuan May 28 '18 at 07:05