Python:“"未定义 [英] Python: ' ' is not defined

查看:122
本文介绍了Python:“"未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

# This program makes the robot calculate the average amount of light in a simulated room

from myro import *
init("simulator")

from random import*

def pressC():
    """ Wait for "c" to be entered from the keyboard in the Python shell """
    entry = " "
    while(entry != "c"):
        entry = raw_input("Press c to continue. ")
    print("Thank you. ")
    print

def randomPosition():
    """ This gets the robot to drive to a random position """
    result = randint(1, 2)
    if(result == 1):
        forward(random(), random())
    if(result == 2):
        backward(random(), random())

def scan():
    """ This allows the robot to rotate and print the numbers that each light sensors obtains """
    leftLightSeries = [0,0,0,0,0,0]
    centerLightSeries = [0,0,0,0,0,0]
    rightLightSeries = [0,0,0,0,0,0]
    for index in range(1,6):
        leftLight = getLight("left")
        leftLightSeries[index] = leftLightSeries[index] + leftLight
        centerLight = getLight("center")
        centerLightSeries[index] = centerLightSeries[index] + centerLight
        rightLight = getLight("right")
        rightLightSeries[index] = rightLightSeries[index] + rightLight
        turnRight(.5,2.739)
    return leftLightSeries
    return centerLightSeries
    return rightLightSeries

def printResults():
    """ This function prints the results of the dice roll simulation."""
    print " Average Light Levels "
    print "    L      C      R "
    print "========================="
    for index in range(1, 6):
        print str(index) + " " + str(leftLightSeries[index]) + " " + str(centerLightSeries[index]) + " " + str(rightLightSeries[index])

def main():
    senses()
    pressC()
    randomPosition()
    scan() 
    printResults()

main()

因此,我在运行程序时遇到此错误.

So, I am getting this error when I run my program.

NameError: global name 'leftLightSeries' is not defined

我了解我在做与return语句有关的错误操作.我不确定是否只能在用户定义函数的末尾返回一个变量.如果那是真的,那我应该分开scan():函数.无论如何,对于如何解决此错误,我将不胜感激.另外,这是我成功完成程序后要寻找的结果: 单击此处

I understand that I must be doing something wrong related to the return statement. I'm not sure if I can only return one variable at the end of a user-defined function. If that were to be true, then I should probably separate the scan(): function. Anyways, I would appreciate any help on how to fix this error. Also, this is the result that I am looking for when I successfully complete my program: Click Here

我希望完成如图所示的平均值,但目前我不担心它们,仅担心来自光传感器的值列表.我不需要知道这些确切的数字,模拟器中的数字会有所不同.

I am looking to complete the average values like the picture shows, but I am not worried about them at this point, only the list of values from the light sensors. I do not need to reach those exact numbers, the numbers will vary in the simulator.

推荐答案

如果要从scan()返回多个项目,请不要使用三个单独的return语句.相反,请执行以下操作:

If you want to return multiple items from scan(), don't use three separate return statements. Instead, do this:

return leftLightSeries, centerLightSeries, rightLightSeries

此外,在调用函数时,还必须将变量分配给返回的值;它不会自动创建具有相同名称的新局部变量.因此,在main中,像这样调用scan():

Also, when you call the function, you have to assign variable(s) to the returned values; it won't automatically create new local variables with the same names. So in main, call scan() like this:

leftLightSeries, centerLightSeries, rightLightSeries = scan()

这篇关于Python:“"未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆