Python if-else statement order -
my problem similar one: python if-elif statements order answer is:
#this answer given question linked directions = [] if <the obstacle has free tile on right>: directions.append(move_right) if <the obstacle has free tile on left>: directions.append(move_left) if not directions: stop() else: random.choice(directions)()
now, question how input if statements list direction=[] ? valid data type? edit: want know how able apply code above. let's there's object in maze , when reaches intersection code have:
if (a in intersection): a.forward() elif (forward().doesntexist): a.left() elif (left().doesntexist): a.right() ......
but code mean he'd go forward first, left, right, etc. want direction random may go right/forward/left first.
import sys import random # example "free tile" free_tile = (0,5) def move_right(): my_pos = free_tile return my_pos def move_left(): my_pos = free_tile return my_pos def stop(): print 'bye!' sys.exit(0) directions = [] if free_tile == (0,5): print 'moving right' directions.append(move_right()) if free_tile == (0,6): print 'moving left' directions.append(move_left()) elif not directions: print 'no directions...exiting...' stop() else: print random.choice(directions) print 'current location', directions
demo:
moving right (0, 5) current location [(0, 5)]
Comments
Post a Comment