mugnuff
mugnuff

Reputation: 37

How to return the value of a while loop counter

I want to return the counter of the while loops, i and b after every loop repetition to use in another function. I haven't found anything related to returning these values.

def dis(reps, towards, back):
    t = 0  # move towards t times
    b = 0  # move back b times
    i = 0  # repetitions
    n =  1 # counts every step
    while i < reps:
        while t < towards:
            do_something()
            i += 1
            n +=1
        while b < back:
            do_something()
            n += 1
            b += 1
        t = 0
        b = 0
        i += 1

I thought of adding another variable like counter_towards and counter_back and add a value each time but that would not fix my problem since I still would have to return these values every rep. Adding the related functions to a class might work aswlel but that would be lot of work and i thought there might be an easy answer to this question.

Upvotes: 0

Views: 78

Answers (1)

AK47
AK47

Reputation: 31

You have to watch on python Generators. Here's a link!

Upvotes: 1

Related Questions