我的代码很长,我需要帮助冷凝它以使其更加方便。我所拥有的代码是假设我执行了一组指令,让它在侥幸机器人上执行这些指令。我正在使用python。机器人假设使用它的传感器执行以下代码。我需要帮助凝结它。Python冷凝代码
编辑:
我的代码:
from Myro import *
from Graphics import *
init('/dev/tty.IPRE6-366079-DevB')
def markYellow(pic):
for pix in getPixels(pic):
r = getRed(pix)
g = getGreen(pix)
b = getBlue(pix)
if r > 200 and b < 90 and g > 150:
setRed(pix,255)
setGreen(pix,255)
setBlue(pix,255)
else:
setRed(pix,0)
setGreen(pix,0)
setBlue(pix,0)
def pctMarked(pic):
totalPixels = 0
whitePixels = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
whitePixels = whitePixels + 1
totalPixels = totalPixels + 1
result = whitePixels/float(totalPixels)
return result
def findAvgX(pic):
pixelCount = 0
totalXCount = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
x = getX(pix)
totalXCount = totalXCount + x
pixelCount = pixelCount + 1
avgX = totalXCount/float(pixelCount)
return avgX
def turn():
findAvgX(pic)
if wallLocation <= 85:
turnLeft(1,0.25)
elif ballLocation >= 170:
turnRight(1,0.25)
def celebrate():
move(0.25,1)
beep(1,800)
beep(1,1600)
beep(1,800)
stop()
def main():
p = takePicture()
markYellow(p)
pctMarked(p)
while pctMarked(pic) < 0.2:
rotate(1,1)
p = takePicture()
markYellow(p)
pctMarked(p)
turn()
while getObstacle('center')> 1000: # I'm not sure about the number. We can test it tomorrow
forward(1,1)
celebrate()
你可以实现'getRGB'和'setRGB'而不是'getRed','setRed',...?这样,你可以做'r,g,b = getRGB(pix)'和'setRGB(pix,r,g,b)'。 – Blender
你的代码不是很长。事实上,我会说这很短。 – aib
除非这是一个关于您认为可能被python内建函数或某事压缩的特定代码的非常集中的问题,否则我认为像这样的问题(带有工作代码)可能更适合codereview.stackexchange。我会投票结束,我们会看看是否有其他人同意... – mgilson