Python: NameError“”未定义
Python: NameError ' ' is not defined
好的,所以我希望程序的最终结果如下所示...
现在,我不需要那些确切的数字,因为这会将机器人置于模拟中,因此结果应该会有所不同。
这是我的代码:
# 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, centerLightSeries, 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()
leftLightSeries, centerLightSeries, rightLightSeries = scan()
printResults()
main()
而且,当我 运行 我的代码时出现此错误:
Traceback (most recent call last):
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 58, in -toplevel-
main()
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 56, in main
printResults()
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 49, in printResults
print str(index) + " " + str(leftLightSeries[index]) + " " + str(centerLightSeries[index]) + " " + str(rightLightSeries[index])
NameError: global name 'leftLightSeries' is not defined
所以,我很困惑为什么我的 return 语句不起作用,为什么我没有得到我想要的列表。请帮忙。
您在函数 scan() 中定义了 leftLightSeries 和 centerLightSeries。由于它们未在外部作用域(在本例中为全局作用域)中定义,函数 printResults 无法访问它们。
Python 文档在 4.2.2. Resolution of names
部分对其进行了描述
A scope defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks
contained within the defining one, unless a contained block introduces
a different binding for the name.
When a name is used in a code block, it is resolved using the nearest
enclosing scope. The set of all such scopes visible to a code block is
called the block’s environment.
When a name is not found at all, a NameError exception is raised. If
the current scope is a function scope, and the name refers to a local
variable that has not yet been bound to a value at the point where the
name is used, an UnboundLocalError exception is raised.
UnboundLocalError is a subclass of NameError.
leftLightSeries
、centerLightSeries
和 rightLightSeries
未在 printResults
的范围内定义,这就是您收到该错误的原因。
更新您的 printResults
函数以接受这些参数:
def printResults(leftLightSeries, centerLightSeries, rightLightSeries):
稍后,当您调用 printResults
时,将这些变量传入:
def main():
...
printResults(leftLightSeries, centerLightSeries, rightLightSeries)
现在的问题是,您仍然没有在 main 范围内定义这 3 个变量,它们仅在 scan
内定义。你如何让他们离开 scan
?只需将 scan
的 return 值存储在变量中,如下所示:
def main():
senses()
pressC()
randomPosition()
leftLightSeries, centerLightSeries, rightLightSeries = scan()
printResults(leftLightSeries, centerLightSeries, rightLightSeries)
你也可以直接将scan
的输出发送到printResults
而不需要临时变量:printResults(*scan())
您遇到的问题是 scoping 问题。
leftLightSeries
是在函数 scan()
的范围内定义的,这意味着它只能从该函数内访问。所以在函数范围内不可用printResults()
.
您的代码的另一个问题是您有多个 return
永远不会达到的语句:
return leftLightSeries
return centerLightSeries
return rightLightSeries
此代码块将 return leftLightSeries
然后永远不会到达接下来的两个 return
语句。如果想要 return 多个值,您可以考虑的一种选择是 return 一个元组:
return (leftLightSeries, centerLightSeries, rightLightSeries,)
然后在 printResults()
你可以像这样获取你的系列:
leftSeries, centerSeries, rightSeries = scan()
for index in range(0,6):
print str(index) + " " + str(leftSeries[index]) + " " + str(centerSeries[index]) + " " + str(rightSeries[index])
另请注意,range(0,6)
是您想要的,而不是范围 (1,6)。数组从索引 0 开始,range
为您提供从第一个参数到但不包括第二个参数的范围。
好的,所以我希望程序的最终结果如下所示...
现在,我不需要那些确切的数字,因为这会将机器人置于模拟中,因此结果应该会有所不同。
这是我的代码:
# 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, centerLightSeries, 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()
leftLightSeries, centerLightSeries, rightLightSeries = scan()
printResults()
main()
而且,当我 运行 我的代码时出现此错误:
Traceback (most recent call last):
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 58, in -toplevel-
main()
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 56, in main
printResults()
File "C:/Users/Owner-pc/Desktop/Computer Programming 1/Mod05/Code/Created/AverageLight.py", line 49, in printResults
print str(index) + " " + str(leftLightSeries[index]) + " " + str(centerLightSeries[index]) + " " + str(rightLightSeries[index])
NameError: global name 'leftLightSeries' is not defined
所以,我很困惑为什么我的 return 语句不起作用,为什么我没有得到我想要的列表。请帮忙。
您在函数 scan() 中定义了 leftLightSeries 和 centerLightSeries。由于它们未在外部作用域(在本例中为全局作用域)中定义,函数 printResults 无法访问它们。
Python 文档在 4.2.2. Resolution of names
部分对其进行了描述A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name.
When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment.
When a name is not found at all, a NameError exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an UnboundLocalError exception is raised. UnboundLocalError is a subclass of NameError.
leftLightSeries
、centerLightSeries
和 rightLightSeries
未在 printResults
的范围内定义,这就是您收到该错误的原因。
更新您的 printResults
函数以接受这些参数:
def printResults(leftLightSeries, centerLightSeries, rightLightSeries):
稍后,当您调用 printResults
时,将这些变量传入:
def main():
...
printResults(leftLightSeries, centerLightSeries, rightLightSeries)
现在的问题是,您仍然没有在 main 范围内定义这 3 个变量,它们仅在 scan
内定义。你如何让他们离开 scan
?只需将 scan
的 return 值存储在变量中,如下所示:
def main():
senses()
pressC()
randomPosition()
leftLightSeries, centerLightSeries, rightLightSeries = scan()
printResults(leftLightSeries, centerLightSeries, rightLightSeries)
你也可以直接将scan
的输出发送到printResults
而不需要临时变量:printResults(*scan())
您遇到的问题是 scoping 问题。
leftLightSeries
是在函数 scan()
的范围内定义的,这意味着它只能从该函数内访问。所以在函数范围内不可用printResults()
.
您的代码的另一个问题是您有多个 return
永远不会达到的语句:
return leftLightSeries
return centerLightSeries
return rightLightSeries
此代码块将 return leftLightSeries
然后永远不会到达接下来的两个 return
语句。如果想要 return 多个值,您可以考虑的一种选择是 return 一个元组:
return (leftLightSeries, centerLightSeries, rightLightSeries,)
然后在 printResults()
你可以像这样获取你的系列:
leftSeries, centerSeries, rightSeries = scan()
for index in range(0,6):
print str(index) + " " + str(leftSeries[index]) + " " + str(centerSeries[index]) + " " + str(rightSeries[index])
另请注意,range(0,6)
是您想要的,而不是范围 (1,6)。数组从索引 0 开始,range
为您提供从第一个参数到但不包括第二个参数的范围。