Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested loop provides unwanted action

I am trying make a nested loop so a while loop gets repeated n times. An object is supposed to drive a distance in i steps for n repetitions. Axis and speed are irrelevant for the loop.
What I don’t understand is why the first i in i = i + 1 is not used, since just i + 1 doesn’t seem to do the job either.

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
    

My current output is:

50000625 nm Gefahren
1
50000625 nm Gefahren
2
-99999375 nm Gefahren
-9768750 nm Gefahren


Whereas Gefahren equals the distance driven and is returned by the fahren() function. So the inner while loop works just as intended since the distance is divided into steps and whichi are driven correctly, but the outer while loop doesn’t seem to work as it’s just trying to drive the whole way back again and again.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m using a stepmotor and I’m trying to divide a distance it has to drive into steps and let it drive those steps until the original distance is reached and then let it drive the whole distance back without those steps. I added the outer while loop to add repetitions since I want to to this process i times to measure inaccuracy of the step motor.

>Solution :

Your i = i + 1 needs to be indented to run within the while loop. That way, i will increase until it exceeds reps and your outer while loop will stop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading