# 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[0]
pRG     = p - ori[0]
pDW     = p - ori[2]

nearPoint   = rayLen
bestRay     = None

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

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

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

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

if LF and llv[0]&gt;-10 : llv[0] -= 0.5
if RG and llv[0]&lt;+10 : llv[0] += 0.5
if SPACE             : llv[2]  = 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[0] :
dist=(ray[1]-p).magnitude
if dist &lt; nearPoint:
nearPoint   = dist
bestRay     = ray

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

ray = ow.rayCast(pDW, ow, rayLen, "ground")
if ray[0] :
dist = (ray[1]-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[0] :
dist=(ray[1]-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;)