Python:无法解决全局变量错误

时间:2015-04-14 10:29:53

标签: python raspberry-pi

即使我将变量声明为全局变量,我也会将此错误视为未定义的全局名称。我不确定我是否已将变量声明为正确的全局变量。

我在这个程序中尝试做的是从函数lux()我将传感器的原始数据读入var。传感器原始数据有两个值,亮度和IR。我将它们分别分配给channel0和channel1。 在函数getLux()中,我使用channel0和channel1来计算增益值。

请帮我解决此错误

#!/usr/bin/python

import time
import smbus

from smbus import SMBus

address = 0x39
control = 0x03
tsl = SMBus(1)
pause = 0.08

def lux():
    #global channel0
    #global channel1
    print "Workking"
    tsl.write_byte(address, 0x80)
    tsl.write_byte(address, control)

    var = [0, 0, 0, 0]
    var = tsl.read_i2c_block_data(0x39, 0x8d)
    channel0 = ((var[1]<<8) + var[0])
    channel1 = ((var[3]<<8) + var[2])

    print "total light %5d     IR light %5d" % (channel0, channel1)
    time.sleep(2)
    return (channel0, channel1)

def setGain(gain):
    if (gain == 1):
            tsl.write_byte_data(address, 0x81, 0x01)
    elif (gain == 16):
            tsl.write_byte_data(address, 0x81, 0x11)
    time.sleep(pause)

def getLux(gain):
    global channel0
    global channel1

    if (gain == 1 or gain == 16):
            setGain(gain)
            ambient = lux(channel0)
            IR = lux(channel1)

    elif (gain == 0):
            setGain(16) #first try high gain
            ambient = channel0
            if (ambient < 65535):
                    IR = channel1
            if (ambient >= 65535):
                    setGain(1) #set low gain
                    ambient = channel0
                    IR = channel1

    if (gain == 1):
            ambient *= 16 #scale 1x to 16x
            IR *= 16

   if (float(ambient == 0)):
            ratio = 9999

    else:
            ratio = (IR / float(ambient))

    if (ratio >= 0) & (ratio <= 0.52):
            tlux = (0.0315 * ambient) - (0.0593 * ambient * (ratio**1.4))
    elif (ratio <= 0.65):
            tlux = (0.299 * ambient) - (0.0291 * IR)
    elif (ratio <= 0.80):
            tlux = (0.0157 * ambient) - (0.018 * IR)
    elif (ratio <= 1.3):
            tlux = (0.00338 * ambient) - (0.0026 * IR)
    elif (ratio > 1.3):
            tlux = 0

    return tlux



while True:

    print "LUX High Gain ", getLux(16)
    print "LUX Low Gain ", getLux(1)
    print "LUX AUTO Gain ", getLux()

Traceback (most recent call last): File "lux2.py", line 85, in <module> print "LUX High Gain ", getLux(16) File "lux2.py", line 45, in getLux ambient = lux(channel0) NameError: global name 'channel0' is not defined

3 个答案:

答案 0 :(得分:0)

您尚未在任何地方声明channel0。你已经在lux()中声明了它,它不会使它成为一个全局变量。

你应该在所有函数范围之外用一个初始值声明它,在这里:

channel0= 0
address = 0x39
control = 0x03
tsl = SMBus(1)
pause = 0.08

而且,要更改其值,您应该声明

global channel0 

里面的lux()函数。

在调用getLux()之前,请先注意先调用lux(),否则channel0将始终为0。 另外,尽量不要使用全局变量,因为它被认为是一个糟糕的编程实践。

答案 1 :(得分:0)

问题是,我看到你还没有初始化全局变量。你需要在getLux函数中初始化全局变量,如。

global channel0
global channel1

channel0 = 0
channel1 = 0

这将解决问题。

而不是使用全局关键字。你可以全局声明channel0,channel1来访问,分配&amp;重新分配任何功能。

答案 2 :(得分:0)

您可以将channel0和channel1作为属性分配给getLux函数:

getLux(gain, channel0, channel1)

然后执行代码可能如下所示:

while True:
    channels = lux() # This creates a tuple with channel variables
    print "LUX High Gain ", getLux(16, channels[0], channels[1])
    print "LUX Low Gain ", getLux(1, channels[0], channels[1])
    print "LUX AUTO Gain ", getLux(0, channels[0], channels[1])

这样您就不需要将它们声明为全局通常被视为不安全的做法。