Итак, я хочу, чтобы конечный результат моей программы, чтобы посмотреть, как это ...Python: NameError «» не определен
Теперь я не нужны эти точные цифры, потому что это будет помещать робота в симуляцию, поэтому результаты должны отличаться.
Вот мой код:
# 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
Итак, я запутался, почему мое возвращение заявление не работает , и почему я не получаю список, который я хочу. Пожалуйста помоги.
Вы отправили этот вопрос менее чем через 12 часов после публикации того же вопроса [здесь] (http://stackoverflow.com/questions/38404825/python-is-not-defined). SO - это _community_ - пожалуйста, не относитесь к нему как к механическому турку. – zeantsoi