saikumar
saikumar

Reputation: 179

local variable 'count' defined in enclosing scope on line 10 referenced before assignment

I am having an issue with the following code of Python 3 (the line at 5th position from bottom count=count+1):

import base64
import json
from google.cloud import iot_v1
import os
from twilio.rest import Client

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
count = 0


def device_config(config):
    client = iot_v1.DeviceManagerClient()
    name = client.device_path(<project_ID>,
                              <Region>,  <Registry>, <Device_ID>)
    binary_data = bytes(config, 'utf-8')
    client.modify_cloud_to_device_config(name, binary_data)


def hello_pubsub(event, context):
    if 'data' in event:
        data = event['data']
        data = base64.b64decode(data)
        data = data.decode('utf-8')
        data = json.loads(data)

        temperature = float(data['temperature'])

        if temperature > 25.0:
            device_config("ledon")
            if count < 1:
                client.calls.create( \
                    url=<URL>,
                    to=os.environ['TWILIO_TO'],
                    from_=os.environ['TWILIO_FROM'])
                count = count+1
        else:
            device_config("ledoff")
    else:
        print("Data is not present!")

This function will be called continuously (imagine like an infinite loop will call this function). I want to update count by 1 when first time temperature goes above 25 and for later calls, how high it may go, LED should be on, but calling shouldnot be done

Upvotes: 0

Views: 10179

Answers (4)

Ferdous Hussain
Ferdous Hussain

Reputation: 73

You are trying to call a local variable into another function. You also need to pass the variable into the function. Hope this helps!

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
global count = 0



def hello_pubsub(event, context,count):

Upvotes: 1

hiro protagonist
hiro protagonist

Reputation: 46921

in hello_pubsub(event, context) you assign to count in the line count = count+1. which makes python look for count in the function scope (where it has not been defined and therefore lookup on the right-hand-side [count+1] fails).

this might be a worhty read: https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html#variable-scope-and-lifetime

you need to tell python that you mean the global count:

def hello_pubsub(event, context):
    global count
    ...

probably cleaner would be:

def hello_pubsub(count, event, context):
    ...

and then call it using:

hello_pubsub(count, event, context)

Upvotes: 8

Rob Streeting
Rob Streeting

Reputation: 1735

You should pass count into the function as a parameter, and increment it in the loop that will call the function. If you absolutely need to assign to it within the function, you can use the statement global count before you do the assignment.

Upvotes: 1

clubby789
clubby789

Reputation: 2733

You are referencing count inside a function, whereas it is defined outside. You should pass count as a parameter.

Upvotes: 1

Related Questions