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


Groups > comp.lang.python > #37540

Re: Decrease for loop by one

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <matt17@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; 'stops': 0.07; 'python': 0.09; 'arg': 0.09; 'behave': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'suggest': 0.11; '*before*': 0.16; '24,': 0.16; 'egg': 0.16; 'helps!': 0.16; 'inclusive.': 0.16; 'integers,': 0.16; 'works...': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'integer': 0.17; 'thu,': 0.17; 'jan': 0.18; 'trying': 0.21; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'statement': 0.23; 'this:': 0.23; 'random': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'thanks!': 0.26; 'strongly': 0.27; "doesn't": 0.28; 'went': 0.28; 'chris': 0.28; 'array': 0.29; 'worked': 0.30; 'figure': 0.30; 'point': 0.31; 'languages': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'pm,': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'but': 0.36; 'rather': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'step': 0.39; 'think': 0.40; 'your': 0.60; 'range': 0.60; 'more': 0.63; 'skip:c 50': 0.66; "'for'": 0.84; '2013': 0.84; 'route': 0.84; 'safer': 0.91
X-Received by 10.50.191.131 with SMTP id gy3mr157978igc.1.1359004237467; Wed, 23 Jan 2013 21:10:37 -0800 (PST)
Newsgroups comp.lang.python
Date Wed, 23 Jan 2013 21:10:37 -0800 (PST)
In-Reply-To <mailman.941.1359003563.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=74.209.38.106; posting-account=oSMPUwoAAACJMDnkakz0WqqxREXMM3aV
References <7694aab5-7926-482e-975a-7685c7b94351@googlegroups.com> <mailman.941.1359003563.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 74.209.38.106
MIME-Version 1.0
Subject Re: Decrease for loop by one
From Milter Skyler <matt17@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.943.1359004246.2939.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359004246 news.xs4all.nl 6933 [2001:888:2000:d::a6]:43816
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37540

Show key headers only | View raw


On Wednesday, January 23, 2013 9:59:13 PM UTC-7, Chris Angelico wrote:
> On Thu, Jan 24, 2013 at 3:39 PM, Milter Skyler <> 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

Thanks! That worked perfect, I was trying to figure that out but instead I just went down the hard route I went down.

Back to comp.lang.python | Previous | NextPrevious 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