确定环境光水平的量

时间:2015-05-20 22:11:44

标签: python for-loop sensor light ambient

对于这个程序,我正在使用python。

我应该将机器人移动到一个随机位置并将其旋转360度。我必须将所有三个光传感器的15个读数分配给三个不同的列表。

然后,我必须计算每个传感器的光值的平均值,然后确定每个传感器的最大值和最小值。

然后,我必须计算并显示来自所有三个传感器的光值的平均值,并让它宣布其周围的平均光值以及它是太亮还是太暗。

我相信我已经完成了所有这些,但我的计划不起作用。例如,我的机器人在随机位置移动然后转动360度,但程序就停止了,我无法弄清楚原因。

出现错误消息:

max() arg is an empty sequence 

如果有人能帮助我弄清楚我做错了什么就会很棒! 非常感谢你!!!

这是我的计划:

from myro import *
init("simulator")

def pressC():
    entry = " "
    while(entry != "c"):
        entry = raw_input("Press c to continue: ")

def movement():
    rightSensor = []
    centerSensor = []
    leftSensor = []
    return rightSensor
    return centerSensor
    return leftSensor

    for index in range(1,15):
        rightSensor.append(getLight("right"))
        centerSensor.append(getLight("center"))
        leftSensor.append(getLight("left"))

def average():
    rightSensor = []
    centerSensor = []
    leftSensor = []
    sumRight = 0
    sumCenter = 0
    sumLeft = 0

    for index in range(len(rightSensor)):
        sumRight = sumRight + rightSensor[index]
        averageRight = sumRight / 15

    for index in range(len(centerSensor)):
        sumCenter = sumCenter + centerSensor[index]
        averageCenter = sumCenter / 15
        while averageCenter > 100:
            speak("Its too dark. I need light")
        else:
            speak("Its so bright")

    for index in range(len(leftSensor)):
        sumLeft = sumLeft + leftSensor[index]
        averageLeft = sumLeft / 15

    max(rightSensor)
    min(rightSensor)
    max(centerSensor)
    min(centerSensor)
    max(leftSensor)
    min(leftSensor)

    print "Left Avg: " + str(averageLeft), "Max: " + str(leftSensor), "Min: " + str(leftSensor)
    print "Center Avg: " + str(averageCenter), "Max: " + str(centerSensor), "Min: " + str(centerSensor)
    print "Right Avg: " + str(averageRight), "Max: " + str(rightSensor), "Min: " + str(rightSensor)


def main():
    pressC()
    forward(1,1)
    rotate(1)
    wait(7)
    stop()
    movement()
    average()

main()

1 个答案:

答案 0 :(得分:2)

这是代码的相关部分:

def average():
    rightSensor = []
    (do stuff that does not change rightSensor)
    max(rightSensor)

难怪它是空的。