Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97922

Re: Resetting the Range start in the middle of a loop

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Resetting the Range start in the middle of a loop
Date 2015-10-23 17:15 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <n0dq07$2tm$1@reader1.panix.com> (permalink)
References <ea58da60-3fb1-46e1-91db-ed48beebfd5c@googlegroups.com>

Show all headers | View raw


In <ea58da60-3fb1-46e1-91db-ed48beebfd5c@googlegroups.com> bigred04bd3@gmail.com writes:

> I am wanting to reset a Range start while a loop is running.
> here is what I have:

> s = 1
> for i in range(s, 1000):
>     # do stuff
>         for r in range(i, 1000):
>             # do stuff
>             s = s + 100  #example
>             break

> I've also tried to update the i as well, but it just continues on to
> the next number as if it was never updated.

You can't do this.  The range is only calculated once, and changing s or i
afterwards is too late to have any effect.

You could use a while loop instead, like this:

    i = 1
    while i <= 1000:

        # do stuff...

        if some_condition:
            # skip ahead in the loop
            i = i + 100

        else:
            # move i ahead normally
            i = i + 1

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Resetting the Range start in the middle of a loop bigred04bd3@gmail.com - 2015-10-23 08:42 -0700
  Re: Resetting the Range start in the middle of a loop John Gordon <gordon@panix.com> - 2015-10-23 17:15 +0000
  Re: Resetting the Range start in the middle of a loop Peter Pearson <pkpearson@nowhere.invalid> - 2015-10-23 17:23 +0000
  Re: Resetting the Range start in the middle of a loop Steven D'Aprano <steve@pearwood.info> - 2015-10-24 11:05 +1100
  Re: Resetting the Range start in the middle of a loop Nobody <nobody@nowhere.invalid> - 2015-10-24 03:13 +0100
  Re: Resetting the Range start in the middle of a loop bigred04bd3@gmail.com - 2015-10-23 19:31 -0700

csiph-web