如何让python对多个变量进行加法和除法?

How to get python to add and divide multiple variables?

问候 python 程序员。我正在构建一个 raspberry pi 项目,旨在在我的花园中使用湿度传感器。我的程序设计用于读取三十秒的读数,然后将它们全部平均为一个数字。它使用四个传感器并耗尽 python3.5 传感器都工作得很好,但我在让 python 计算平均输入时遇到问题。我认为它正在添加变量,但它似乎并没有将它们分开。这是代码:


        TEST_SOIL()
        outpuT = str(output)
        print("Moisture:", outpuT)
        GPIO.output(buzzer,GPIO.HIGH)
        sleep(0.1) # Delay in seconds
        GPIO.output(buzzer,GPIO.LOW)
        sleep(0.1)
        GPIO.output(buzzer,GPIO.HIGH)
        sleep(0.1) # Delay in seconds
        GPIO.output(buzzer,GPIO.LOW)
        print("TESTING SOIL")
        o1 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o2 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o3 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o4 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o5 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o6 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o7 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o8 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o9 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o10 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o11 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o12 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o13 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o14 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o15 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o16 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o17 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o18 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o19 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o20 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o21 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o22 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o23 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o24 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o25 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o26 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o27 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o28 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o29 = (output + output2 + output3 + output4/4)
        sleep(1)
        TEST_SOIL()
        o30 = (output + output2 + output3 + output4/4)
        sleep(1)
        o = (o1 + o2 + o3 + o4 + o5 + o6 + o7 + o8 + o9 + o10 + o11 + o12 + o13 + o14 + o15 + o16 + o17 + o18 + o19 + o20 + o21 + o22 + o23 + o24 + o25 + o26 + o27 + o28 + o29 + o30)
        o = (o/30) 

如果有人想知道 "TEST_SOIL()" 是什么,那是我之前在程序中创建的自己的函数。在这里定义:

def TEST_SOIL():
   output = analogInput(0) # Reading from CH0
   output = interp(output, [0, 1023], [100, 0])
   output = int(output)
   output = (output - 12.5)
   output2 = analogInput(1) # Reading from CH0
   output2 = interp(output2, [1, 1023], [100, 1])
   output2 = int(output2)
   output2 = (output2 - 12.5)
   output3 = analogInput(2) # Reading from CH0
   output3 = interp(output3, [2, 1023], [100, 2])
   output3 = int(output3)
   output3 = (output3 - 12.5)
   output4 = analogInput(3) # Reading from CH0
   output4 = interp(output4, [3, 1023], [100, 3])
   output4 = int(output4)
   output4 = (output4 - 12.5)

我需要指出的是,此代码还控制一个小蜂鸣器以消除与 "GPIO.output(buzzer,high/low)" 文本的任何混淆。如果有人能告诉我如何修复这段代码那就太好了。
:)

(output + output2 + output3 + output4/4) 更改为 (output + output2 + output3 + output4)/4

您的代码首先将最后一个变量 (ouotput4) 除以四,然后将它们相加。

(编辑:忘记列表没有平均函数。抱歉。您可以对某些列表使用 sum(),然后将其除以该列表的 len()

我冒昧地重构了你的代码。

我假设 4 个不同的模拟读数需要如此并且没有触及它们,但除此之外......

  • test_soil 现在 returns 读数的 4 元组。
  • average_output() 对任何可迭代的数字进行平均。
  • beep() 蜂鸣器发出蜂鸣声。
  • main 在一个循环中调用上述位,将值收集到一个数组中,然后最终对其进行平均。
def TEST_SOIL():
    output = analogInput(0)  # Reading from CH0
    output = interp(output, [0, 1023], [100, 0])
    output = int(output)
    output = output - 12.5
    output2 = analogInput(1)  # Reading from CH0
    output2 = interp(output2, [1, 1023], [100, 1])
    output2 = int(output2)
    output2 = output2 - 12.5
    output3 = analogInput(2)  # Reading from CH0
    output3 = interp(output3, [2, 1023], [100, 2])
    output3 = int(output3)
    output3 = output3 - 12.5
    output4 = analogInput(3)  # Reading from CH0
    output4 = interp(output4, [3, 1023], [100, 3])
    output4 = int(output4)
    output4 = output4 - 12.5
    return (output, output2, output3, output4)


def average_output(o):
    return sum(o) / len(o)


def beep():
    GPIO.output(buzzer, GPIO.HIGH)
    sleep(0.1)  # Delay in seconds
    GPIO.output(buzzer, GPIO.LOW)


def main():
    ao = average_output(TEST_SOIL())
    print("Initial Moisture:", ao)
    beep()
    beep()
    print("TESTING SOIL")
    values = []
    for x in range(30):
        values.append(average_output(TEST_SOIL()))
        sleep(1)
    final_average = average_output(values)
    print("Final Moisture:", final_average )

这可能会修复你的代码并稍微整理一下 你可以使用 for 循环来简化工作,而不是再次编写相同的东西

for step in range(30):
    TEST_SOIL()
    op_avg = (output1 + output2 + output3 + output4) / 4.0
    op = op + op_avg
    sleep(1)
op = op/30

希望对您有所帮助