nameerror - Python: ' ' is not defined -
here code:
# program makes robot calculate average amount of light in simulated room myro import * init("simulator") random import* def pressc(): """ wait "c" entered keyboard in python shell """ entry = " " while(entry != "c"): entry = raw_input("press c continue. ") print("thank you. ") print def randomposition(): """ gets robot drive random position """ result = randint(1, 2) if(result == 1): forward(random(), random()) if(result == 2): backward(random(), random()) def scan(): """ allows robot rotate , print numbers 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] 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(): """ function prints results of dice roll simulation.""" print " average light levels " print " l c r " print "=========================" 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, getting error when run program.
nameerror: global name 'leftlightseries' not defined
i understand must doing wrong related return statement. i'm not sure if can return 1 variable @ end of user-defined function. if true, should separate scan(): function. anyways, appreciate on how fix error. also, result looking when complete program: click here
i looking complete average values picture shows, not worried them @ point, list of values light sensors. not need reach exact numbers, numbers vary in simulator.
if want return multiple items scan()
, don't use 3 separate return
statements. instead, this:
return leftlightseries, centerlightseries, rightlightseries
also, when call function, have assign variable(s) returned values; won't automatically create new local variables same names. in main
, call scan()
this:
leftlightseries, centerlightseries, rightlightseries = scan()
Comments
Post a Comment