我画的是一片空白,但我确实认为手动编写这段代码似乎有些过分。我可以使用“for 循环”来缩短它并增加下面的变量吗?
pos = 1000
m1.setAsHome() # set position as zero for all of the commands
m1.goTo(pos) # move to the original position oof the limit switch
while(m1.isBusy()):
continue
m1.free() # reset the motor
while(m2.isBusy()):
continue
m2.setAsHome() # set position as zero for all of the commands
m2.goTo(pos) # move to the original position oof the limit switch
while(m2.isBusy()):
continue
m2.free() # reset the motor
我的直觉是这样的:
for i in range(4):
m = 0
print m[i].setAsHome()
当然这会产生错误。很抱歉新手问题,但我相信一定有办法缩短这个问题。此外,上面的代码继续包含 4 个电机。谢谢。
最佳答案
您需要一个电机列表:
motors = [m1, m2, m3, m4]
所以你可以使用 for 循环:
for motor in motors:
motor.setAsHome()
motor.goTo(pos)
关于python - 使用 For 循环缩短重复代码以递增变量。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606920/