我正在嘗試制作一個嵌套回圈,以便 while 回圈重復 n 次。一個物體應該以 i 步驅動一段距離,重復 n 次。軸和速度與回圈無關。我不明白為什么 i = i 1 中的第一個 i 沒有被使用,因為只是 i 1 似乎也沒有完成這項作業。
def steptofull(reps, steps, speed, axis, distance):
i = 0
n = 0
strecke = distance
# rdistance = 0
while i < reps:
while n < steps:
distance = strecke // steps # division ohne rest
# mod = strecke % steps # rest der division, wird evtl addiert als fehler/rest
fahren(speed, axis, distance)
time.sleep(1)
# rdistance = rdistance rmoved
flushbuffer()
n = n 1
print(n)
distance = strecke
fahren(speed, axis, -distance)
time.sleep(1)
i = i 1
我目前的輸出是:
50000625 nm Gefahren
1
50000625 nm Gefahren
2
-99999375 nm Gefahren
-9768750 nm Gefahren
而 Gefahren 等于行駛距離,由 fahren() 函式回傳。因此,內部 while 回圈按預期作業,因為距離被分成多個步驟并且驅動正確,但外部 while 回圈似乎不起作用,因為它只是試圖一次又一次地驅動整個程序。
我正在使用步進電機,我試圖將它必須驅動的距離分成多個步驟,讓它驅動這些步驟,直到達到原始距離,然后讓它在沒有這些步驟的情況下驅動整個距離。我添加了外部while回圈來添加重復,因為我想在這個程序中多次測量步進電機的不準確性。
uj5u.com熱心網友回復:
您i = i 1需要縮進才能在while回圈中運行。這樣,i將增加直到超過reps并且您的外部 while 回圈將停止。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/529293.html
上一篇:將字串正確拆分為陣列陣列
