 # how using a definition?

I have this code with clearly 3 repetitive parts

is just the case then use a definition (def)

but as I do in this case?

I use 3 rayCast() and need to get only the more short (but can also return nothing)

``````
import bge

def player(cn):
ow=cn.owner

key=bge.logic.keyboard.events
ev=bge.events

LF      = key[ev.LEFTARROWKEY]
RG      = key[ev.RIGHTARROWKEY]
SPACE   = key[ev.SPACEKEY]

llv     = ow.localLinearVelocity

ori     = ow.worldOrientation
p       = ow.worldPosition

rayLen  = 2.0
pLF     = p + ori
pRG     = p - ori
pDW     = p - ori

nearPoint   = rayLen
bestRay     = None

##################################
ray = ow.rayCast(pLF, ow, rayLen, "ground")
if ray :
dist=(ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray

ray = ow.rayCast(pRG, ow, rayLen, "ground")
if ray :
dist=(ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray

ray = ow.rayCast(pDW, ow, rayLen, "ground")
if ray :
dist = (ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray
##################################

if bestRay:
hitNormal = bestRay
ow.alignAxisToVect(hitNormal, 2, 0.15)

if LF and llv&gt;-10 : llv -= 0.5
if RG and llv&lt;+10 : llv += 0.5
if SPACE             : llv  = 10

``````

When you have repetitive code like that you would be best to use a loop statement, like the for loop:

``````ray = ow.rayCast(pLF, ow, rayLen, "ground")
if ray :
dist=(ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray

ray = ow.rayCast(pRG, ow, rayLen, "ground")
if ray :
dist=(ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray

ray = ow.rayCast(pDW, ow, rayLen, "ground")
if ray :
dist = (ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray
``````

Becomes

``````for point in [pLF, pRG, pDW]:
ray = ow.rayCast(point, ow, rayLen, "ground")
if ray :
dist=(ray-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray
``````

The for statement at the top will assign each of [pLF, pRG, pDW] to the point variable and then execute the contained beneath it.

Indeed, it now seems very simple solution,
I had not thought of using a list

thank you;)