Python 如何在main中调用这些变量?

Python 如何在main中调用这些变量?,python,function,python-3.x,raspberry-pi2,Python,Function,Python 3.x,Raspberry Pi2,我有这个代码,它运行并将GPIOS 7,11,13,15设置为我的Raspberry Pi 2HIGH或LOW,这样我就可以相应地寻址16多路复用器通道,然后通过和MCP3002 SPI读取模拟电压,并返回是否有按键或按键释放,加上那是哪把钥匙。代码如下: def findpress(keypressed, keyreleased, key): x=0 while True: binary_x="{0:04b}".format(x) GPIO.ou

我有这个代码,它运行并将
GPIOS 7,11,13,15
设置为我的
Raspberry Pi 2
HIGH
LOW
,这样我就可以相应地寻址16多路复用器通道,然后通过和
MCP3002 SPI
读取模拟电压,并返回是否有按键或按键释放,加上那是哪把钥匙。代码如下:

def findpress(keypressed, keyreleased, key):
    x=0
    while True:
        binary_x="{0:04b}".format(x)
        GPIO.output(7, binary_x[0])
        GPIO.output(11, binary_x[1])
        GPIO.output(13, binary_x[2])
        GPIO.output(15, Binary_x[3])
        channeldata_1 = read_mcp3002(0) # get CH0 input
        channeldata_2 = read_mcp3002(0) # get CH0 input
        channeldata_3 = read_mcp3002(0) # get CH0 input
        channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
        #
        # Voltage = (CHX data * (V-ref [= 3300 mV] * 2 [= 1:2 input divider]) / 1024 [= 10bit resolution]
        #
        voltage = int(round(((channeldata * vref * 2) / resolution),0))+ calibration
        if DEBUG : print("Data (bin)    {0:010b}".format(channeldata))
        if x==15 :      # some problem with this sensor so i had to go and twicked the thresshold
            voltage = voltage - 500
        if voltage<=2500 and keyreleased==True:
            return keypressed=True
            return key=x+1
        if voltage<=2500 and keyreleased==False
            return keypressed=True
            return key=x+1
        if voltage>2500 and keypressed==True:
            x=x+1
            return keyreleased==True
        if x == 15:
            x=0
def findpress(按键、释放、按键):
x=0
尽管如此:
二进制_x=“{0:04b}”。格式(x)
GPIO.output(7,二进制_x[0])
GPIO.output(11,二进制_x[1])
GPIO.output(13,二进制_x[2])
GPIO.output(15,二进制_x[3])
channeldata_1=读取mcp3002(0)#获取CH0输入
channeldata_2=读取mcp3002(0)#获取CH0输入
channeldata_3=读取mcp3002(0)#获取CH0输入
channeldata=(channeldata_1+channeldata_2+channeldata_3)/3
#
#电压=(CHX数据*(V-ref[=3300 mV]*2[=1:2输入分压器])/1024[=10位分辨率]
#
电压=整数(四舍五入((信道数据*vref*2)/分辨率),0)+校准
如果调试:打印(“数据(bin){0:010b}”。格式(channeldata))
如果x==15:#这个传感器有问题,所以我不得不去拧一下油门
电压=电压-500

如果电压则向该函数传递三个参数,可能来自主过程

使用元组解包,只需返回一个元组:

print(foo(1, 2, 3)) # should print: 2, 4, 0

# unpacking the tuple like so:
x, y, z = foo(1,2,3)
print x, y, z

def foo(x, y, z):
    x +=1
    y +=2
    z = 0
在代码中,您可以简单地返回一些组合,如:

return keypressed, keyreleased, key
请注意,像这样的多个return语句不会像您预期的那样执行,而是在第一个
return
之后停止,而不是按顺序返回两个值:

        return keypressed=True
        return key=x+1           # This statement will NOT execute
还要注意,您当前正在实现的这种表示法将进行计算并返回一个truthy值:

return keypressed=False # returns True if keypressed==False else False

如果这是您的意图,那么好吧,否则您应该在返回语句之前进行赋值。代码的工作方式,特别是返回语句看起来似乎没有意义,我不打算对它们进行故障排除。

我如何在main中调用这些变量?这是我的问题!我按下了键,keyreleased和key变量表示此函数正在返回。对吗?我如何在main中询问它们?假设keypressed==True,则执行此操作?为什么在return语句中有赋值?我认为您需要更清楚地了解希望从函数返回的内容。值或传递事件发生的值表示事件发生d表示按键按下和按键释放。按键的值。