【问题标题】:syntax error in python with arrays and for loop with if elif?带有数组的python中的语法错误和带有if elif的for循环?
【发布时间】:2016-05-26 02:17:46
【问题描述】:

这是我在 python 中的校准代码。我有 16 个模拟传感器连接到我的树莓派 2, GPIO 7、11、13、15 控制 16 对 1 多路复用器,我将模拟信号馈送到 MCP3002 让我的树莓派读取它们。 我知道这段代码可以工作,因为直到我写了校准代码,一切都很顺利。但仅仅因为每个传感器的读数不同,单个数字作为阈值是不够的。 所以我创建了 2 个整数数组和一个 for 循环来通过每个传感器, 读取 3 次,获取平均价格并将其存储到校准最大数组中。然后打印一条消息并在它开始前给你 3 秒,并为这个传感器做同样的事情,这样它就可以去存储可以从这个传感器获得的最小值并将其存储到校准最小数组中,这样我就可以将它用作参考我的门槛。

但每次我去编译到 shell 时都会收到此错误消息。

您的程序有错误: unindent 不匹配任何外部缩进级别

为什么我会收到此错误消息?

calibration_max=array.array('i',(0 for m in range(0,16)))
calibration_min=array.array('i',(0 for n in range(0,16)))
for m in range(0, 16):
    x == m
    if x == 0:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 1:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 2:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 3:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 4:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 5:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 6:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 7:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 8:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 9:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 10:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 11:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 12:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 13:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 14:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 15:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
    # average three readings to get a more stable one
    channeldata_1 = read_mcp3002(0) # get CH0 input
    sleep(0.001)
    channeldata_2 = read_mcp3002(0) # get CH0 input
    sleep(0.001)
    channeldata_3 = read_mcp3002(0) # get CH0 input
    channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
    calibration_max(m) == channeldata
    print("place magnet over sensor No:"(n+1))
    sleep(3)
    # average three readings to get a more stable one
    channeldata_1 = read_mcp3002(0) # get CH0 input
    sleep(0.001)
    channeldata_2 = read_mcp3002(0) # get CH0 input
    sleep(0.001)
    channeldata_3 = read_mcp3002(0) # get CH0 input
    channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
    calibration_min(n) == channeldata
    n=n+1

【问题讨论】:

    标签: arrays python-2.7 if-statement for-loop syntax


    【解决方案1】:

    这是因为第一个 if 和后面的 elif 的缩进级别不同。这是一个问题,因为 python 按缩进级别定义块。

    试试这个:

    calibration_max=array.array('i',(0 for m in range(0,16)))
    calibration_min=array.array('i',(0 for n in range(0,16)))
    for m in range(0, 16):
        x == m
        if x == 0:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 1:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 2:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 3:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 0)
        elif x == 4:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 5:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 6:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 7:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 0)
        elif x == 8:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 9:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 10:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 11:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 0)
            GPIO.output(15, 1)
        elif x == 12:
            GPIO.output(7, 0)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 13:
            GPIO.output(7, 1)
            GPIO.output(11, 0)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 14:
            GPIO.output(7, 0)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        elif x == 15:
            GPIO.output(7, 1)
            GPIO.output(11, 1)
            GPIO.output(13, 1)
            GPIO.output(15, 1)
        # average three readings to get a more stable one
        channeldata_1 = read_mcp3002(0) # get CH0 input
        sleep(0.001)
        channeldata_2 = read_mcp3002(0) # get CH0 input
        sleep(0.001)
        channeldata_3 = read_mcp3002(0) # get CH0 input
        channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
        calibration_max(m) == channeldata
        print("place magnet over sensor No:"(n+1))
        sleep(3)
        # average three readings to get a more stable one
        channeldata_1 = read_mcp3002(0) # get CH0 input
        sleep(0.001)
        channeldata_2 = read_mcp3002(0) # get CH0 input
        sleep(0.001)
        channeldata_3 = read_mcp3002(0) # get CH0 input
        channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
        calibration_min(n) == channeldata
        n=n+1
    

    【讨论】:

    • 它确实有效!谢谢 !我对python一点也不擅长。现在我收到有关数组声明的错误消息。如何在 python 中声明一个数组?
    • 这是我进入终端的内容:文件“./Documents/Examples/spitest2.py”,第 91 行,在 calibration_max=array.array('i',(0 for m in range(0,16))) NameError: name 'array' is not defined
    • 您很可能在文件顶部缺少import array。这将导入包含 python 数组定义的模块。
    【解决方案2】:

    Python 中的代码块由它们的缩进定义,因此 'elif' 缩进应该匹配 'if' 缩进:

    if x == 0:
    ...
    elif x == 1:
    ...
    elif x == 2:
    ...
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      相关资源
      最近更新 更多