が定義されていないが、私のコードです:私は私のプログラムを実行する際に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()
、私はこのエラーを取得しています。
NameError: global name 'leftLightSeries' is not defined
私はreturn文に関連して何か間違っていなければならないことを理解します。ユーザー定義関数の最後に1つの変数しか返せないかどうかはわかりません。それが真実であれば、私はおそらくscan():関数を分離するべきです。とにかく、私はこのエラーを修正する方法についての助けに感謝します。唯一、私は絵が表示さのような平均値を完了するために探していますが、私はこの時点でそれらを心配していないです Click Here
:また、これは私が成功した私のプログラムを完了したときに探しています結果です光センサーからの値のリスト。私はそれらの正確な数に達する必要はありません、数値はシミュレータで異なります。
'leftLightSeries'はあなた'スキャン() '関数内のローカル変数であり、唯一の存在します。したがって、 'printResults()'では使用できません。そのような複数の連続した 'return'呼び出しはどの言語でも意味をなさない。 'return'は実行制御を直ちに元の呼び出し場所に戻します。 'return'の後のコード行は決して実行されません。 –
あなたはscan()からの戻り値を使用していません。しかし、関数から3つの値を返そうとしていた場合は、それらをタプルとして返すことができます。 'return(leftLightSeries、centerLightSeries、rightLightSeries)'です。 – MarkU
最初の 'return'文だけが実行されます(' return leftLightSeries')。複数の値を返す場合は、辞書、タプル、セットまたはリストを使用します。 – jDo