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


Groups > comp.lang.python > #37536

Re: Decrease for loop by one

References <7694aab5-7926-482e-975a-7685c7b94351@googlegroups.com>
Date 2013-01-24 15:59 +1100
Subject Re: Decrease for loop by one
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.941.1359003563.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jan 24, 2013 at 3:39 PM, Milter Skyler <matt17@gmail.com> wrote:
> I made an array to check if the random integer already exists and then I send it to the else statement at which point I want to decrease x by 1 so that it doesn't count as one of the loops. In other languages this works...

A Python 'for' loop doesn't behave like that; you can't just edit the
loop counter. But take a step back: what are you actually trying to
accomplish? As I see it, you're trying to pick ten unique random
values in the range 1-12 inclusive. Try this:

random.sample(range(1,13),10) # note that range() stops *before* its second arg

That'll give you a list of those integers, and it's a lot safer and
more reliable than the try-fail-retry method. Your loop can become:

    for decimal_value in random.sample(range(1,13),10):
            egg = 'A%d.png' % (decimal_value)
            egg = wait(egg)
            click(egg.getCenter().offset(random.randint(-10,10),
random.randint(-10,10)))

By the way, this line simply won't work:

x-1 = x

I think you mean:

x = x-1

But I strongly suggest you copy and paste directly rather than retype;
it's less error-prone.

Hope that helps!

ChrisA

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


Thread

Decrease for loop by one Milter Skyler <matt17@gmail.com> - 2013-01-23 20:39 -0800
  Re: Decrease for loop by one Chris Angelico <rosuav@gmail.com> - 2013-01-24 15:59 +1100
    Re: Decrease for loop by one Milter Skyler <matt17@gmail.com> - 2013-01-23 21:10 -0800
    Re: Decrease for loop by one Milter Skyler <matt17@gmail.com> - 2013-01-23 21:10 -0800

csiph-web