Decrease for loop by one

Discussion in 'Python' started by Milter Skyler, Jan 24, 2013.

  1. I'm using this code with Sikuli so thats why I have click()
    for x in range(0,10):
    decimal_value = random.randint(1,12)
    if myList.count(decimal_value) < 1:
    egg = 'A%d.png' % (decimal_value)
    egg = wait(egg)
    click(egg.getCenter().offset(random.randint(-10,10), random.randint(-10,10)))
    myList.append(decimal_value)

    else:
    print x
    x-1 = x
    print x

    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 but it just comes up with the output:

    3
    2

    9
    8

    10
    9
    Milter Skyler, Jan 24, 2013
    #1
    1. Advertising

  2. 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
    Chris Angelico, Jan 24, 2013
    #2
    1. Advertising

  3. 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.
    Milter Skyler, Jan 24, 2013
    #3
  4. 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.
    Milter Skyler, Jan 24, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Correa

    Performance decrease after migration to .NET

    Christian Correa, Dec 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    342
    Christian Correa
    Dec 1, 2004
  2. Ravi Ambros Wallau
    Replies:
    1
    Views:
    493
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 14, 2006
  3. fitwell
    Replies:
    7
    Views:
    736
    Sid Ismail
    Nov 19, 2003
  4. Clarence
    Replies:
    3
    Views:
    403
    Joris Gillis
    Aug 11, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page