and nowallright and notonrightedgeset the falling directionfallingdirection none if canfallleft and not canfallrightfallingdirection - set the sand to fall left elif not canfallleft and canfallrightfallingdirection set the sand to fall right elif canfallleft and canfallrightboth are possibleso randomly set itfallingdirection random choice((- )check if the sand can "farfall two spaces to the left or right instead of just one spaceif random random( <wide_fall_chancebelowtwoleft (sand[ sand[ nosandbelowtwoleft belowtwoleft not in allsand nowallbelowtwoleft belowtwoleft not in hourglass notonsecondtoleftedge sand[ canfalltwoleft (canfallleft and nosandbelowtwoleft and nowallbelowtwoleft and notonsecondtoleftedgebelowtworight (sand[ sand[ nosandbelowtworight belowtworight not in allsand nowallbelowtworight belowtworight not in hourglass notonsecondtorightedge sand[xscreen_width canfalltworight (canfallright and nosandbelowtworight and nowallbelowtworight and notonsecondtorightedgeif canfalltwoleft and not canfalltworightfallingdirection - elif not canfalltwoleft and canfalltworightfallingdirection elif canfalltwoleft and canfalltworightfallingdirection random choice((- )if fallingdirection =nonethis sand can' fallso move on continue draw the sand in its new positionbext goto(sand[ ]sand[ ]print('end=''erase old sand bext goto(sand[xfallingdirectionsand[ print(sandend=''draw new sand move the grain of sand to its new positionallsand[ (sand[xfallingdirectionsand[ sandmovedonthisstep true sys stdout flush((required for bext-using programs time sleep(pause_lengthpause after this if no sand has moved on this stepreset the hourglasshourglass