わかりまし定義されたので、私は私のプログラムの最終的な結果は次のようになりたくはないが、私はそれらの正確な数は必要ありません。このため、シミュレーションにロボットを置くので、結果は変わるはずです。ここでパイソン:NameError「」は今...</p> <p><a href="https://i.stack.imgur.com/tryyh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tryyh.png" alt="Desired output"></a></p> <p>を
は私のコードです:
# 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文が動作していない理由として混乱していますなぜ私が望むリストが得られないのか、助けてください。
この質問は、同じ質問[ここ](http://stackoverflow.com/questions/38404825/python-is-not-defined)を投稿してから12時間以内に投稿しました。それは機械的なトルコのように扱わないでください。 – zeantsoi