Changing the number of iterations in a for loop

前端 未结 8 1348
死守一世寂寞
死守一世寂寞 2020-12-10 11:27

I have code like this:

loopcount = 3
for i in range(1, loopcount)
   somestring = \'7\'
   newcount = int(somestring)
   loopcount = newcount
8条回答
  •  一整个雨季
    2020-12-10 11:56

    Here is a more complete implementation of the adjustable_range function provided by Joel Cornett.

    def adjustable_range(start, stop=None, step=None):
        if not isinstance(start, int):
            raise TypeError('start')
        if stop is None:
            start, stop = 0, start
        elif not isinstance(stop, int):
            raise TypeError('stop')
        direction = stop - start
        positive, negative = direction > 0, direction < 0
        if step is None:
            step = +1 if positive else -1
        else:
            if not isinstance(step, int):
                raise TypeError('step')
            if positive and step < 0 or negative and step > 0:
                raise ValueError('step')
        if direction:
            valid = (lambda a, b: a < b) if positive else (lambda a, b: a > b)
            while valid(start, stop):
                message = yield start
                if message is not None:
                    if not isinstance(message, int):
                        raise ValueError('message')
                    stop = message
                start += step
    

提交回复
热议问题