Trouble with for loop

Discussion in 'Python' started by Shriphani, Nov 6, 2007.

  1. Shriphani

    Shriphani Guest

    Hello,
    I want to try something like:

    for (a, b, c, d, e, f) in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    ####

    When I do that I get an error:
    TypeError: unpack non-sequence

    My main intention is to state that each of the variables namely a, b,
    c, ## can take value from 1 to 9.
    How do I go about this ?

    Regards,
    Shriphani Palakodety
     
    Shriphani, Nov 6, 2007
    #1
    1. Advertising

  2. Shriphani

    Ant Guest

    On Nov 6, 9:59 am, Shriphani <> wrote:
    ....
    > My main intention is to state that each of the variables namely a, b,
    > c, ## can take value from 1 to 9.
    > How do I go about this ?


    It sounds like you are after something like:

    for var in (a, b, c, d, e, f):
    assert var in [1, 2, 3, 4, 5, 6, 7, 8, 9]

    but it's hard to tell without some more information from you on
    exactly what you are trying to achieve.
     
    Ant, Nov 6, 2007
    #2
    1. Advertising

  3. Shriphani

    Paul Hankin Guest

    Shriphani wrote:
    > Hello,
    > I want to try something like:
    >
    > for (a, b, c, d, e, f) in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    > ####
    >
    > When I do that I get an error:
    > TypeError: unpack non-sequence
    >
    > My main intention is to state that each of the variables namely a, b,
    > c, ## can take value from 1 to 9.
    > How do I go about this ?


    Do you want the variables to be indepenedent?

    for a in range(1, 10):
    for b in range(1, 10):
    for c in range(1, 10):
    for d in range(1, 10):
    for e in range(1, 10):
    for f in range(1, 10):
    ####

    Or all the same?
    for a in range(1, 10):
    b = c = d = e = f = a

    Whatever you're doing though, there's almost certainly a better way.

    --
    Paul Hankin
     
    Paul Hankin, Nov 6, 2007
    #3
  4. Shriphani

    Amit Khemka Guest

    On 11/6/07, Shriphani <> wrote:
    > Hello,
    > I want to try something like:
    >
    > for (a, b, c, d, e, f) in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    > ####
    >
    > When I do that I get an error:
    > TypeError: unpack non-sequence
    >
    > My main intention is to state that each of the variables namely a, b,
    > c, ## can take value from 1 to 9.
    > How do I go about this ?


    An ugly code for it would be ;-) :
    <code>
    for (a, b, c, d, e, f) in zip(*[range(1, 10)]*6):
    print a, b, c, d, e, f
    </code>

    Cheers,

    --
    --
    Amit Khemka
     
    Amit Khemka, Nov 6, 2007
    #4
  5. Shriphani

    Shriphani Guest

    On Nov 6, 3:09 pm, Ant <> wrote:
    > On Nov 6, 9:59 am, Shriphani <> wrote:
    > ...
    >
    > > My main intention is to state that each of the variables namely a, b,
    > > c, ## can take value from 1 to 9.
    > > How do I go about this ?

    >
    > It sounds like you are after something like:
    >
    > for var in (a, b, c, d, e, f):
    > assert var in [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >
    > but it's hard to tell without some more information from you on
    > exactly what you are trying to achieve.


    I want to obtain a number whose first digit "a" is divisible by 1,
    10*b +a is divisible by 2, 10^2*c + 10b + a is divisible by 3 and so
    on.
    I hope my question is a bit clearer now.
    Thanks,
    Shriphani Palakodety
     
    Shriphani, Nov 6, 2007
    #5
  6. Shriphani

    Paul Hankin Guest

    On Nov 6, 10:19 am, Shriphani <> wrote:
    > On Nov 6, 3:09 pm, Ant <> wrote:
    >
    > > On Nov 6, 9:59 am, Shriphani <> wrote:
    > > ...

    >
    > > > My main intention is to state that each of the variables namely a, b,
    > > > c, ## can take value from 1 to 9.
    > > > How do I go about this ?

    >
    > > It sounds like you are after something like:

    >
    > > for var in (a, b, c, d, e, f):
    > > assert var in [1, 2, 3, 4, 5, 6, 7, 8, 9]

    >
    > > but it's hard to tell without some more information from you on
    > > exactly what you are trying to achieve.

    >
    > I want to obtain a number whose first digit "a" is divisible by 1,
    > 10*b +a is divisible by 2, 10^2*c + 10b + a is divisible by 3 and so
    > on.
    > I hope my question is a bit clearer now.


    Any time you want a bunch of variables with similar meanings, use an
    list (or an array in most other languages) instead. It makes it
    clearer what the relationship between the variables is, and it also
    makes it much easier to extend the code (for instance, what if you
    want to add another digit?)

    a + 10*b == 0 (mod 2) => a is even
    a + 10*b + ... + 10^4*e == 0 (mod 5) => a is 0 or 5

    Therefore, a is 0, so only considering digits 1-9 is a mistake.

    So, something like this is probably what you want...

    for n in range(10 ** 5, 10 ** 6):
    digits = map(int, str(n))
    ... test digits

    --
    Paul Hankin
     
    Paul Hankin, Nov 6, 2007
    #6
  7. Shriphani

    Boris Borcic Guest

    Shriphani wrote:
    > On Nov 6, 3:09 pm, Ant <> wrote:
    >> On Nov 6, 9:59 am, Shriphani <> wrote:
    >> ...
    >>
    >>> My main intention is to state that each of the variables namely a, b,
    >>> c, ## can take value from 1 to 9.
    >>> How do I go about this ?

    >> It sounds like you are after something like:
    >>
    >> for var in (a, b, c, d, e, f):
    >> assert var in [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>
    >> but it's hard to tell without some more information from you on
    >> exactly what you are trying to achieve.

    >
    > I want to obtain a number whose first digit "a" is divisible by 1,
    > 10*b +a is divisible by 2, 10^2*c + 10b + a is divisible by 3 and so
    > on.


    And so on ? up to how many digits ?

    10^3 is divisible by 4 and 10^4 is divisible by 5 so that the conditions
    on the fourth and fifth digits boil down to 10^2*c+10b+a being divisible
    by 4 and 5 in supplement to 3, iow divisible by 60. This implies a==0 but you
    seem to say that a must be in [1, 2, 3, 4, 5, 6, 7, 8, 9].


    > I hope my question is a bit clearer now.


    Not really :)

    > Thanks,
    > Shriphani Palakodety
    >
     
    Boris Borcic, Nov 6, 2007
    #7
    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. M. Duijkers
    Replies:
    2
    Views:
    483
    Joe Smith
    Dec 12, 2004
  2. Hypo
    Replies:
    6
    Views:
    448
  3. mmoski
    Replies:
    5
    Views:
    339
    Luc The Perverse
    Feb 3, 2007
  4. Dick Moores

    Trouble getting loop to break

    Dick Moores, Nov 20, 2007, in forum: Python
    Replies:
    11
    Views:
    506
    Fredrik Johansson
    Nov 25, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page