473,287 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Trouble with for loop

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

Nov 6 '07 #1
6 1103
Ant
On Nov 6, 9:59 am, Shriphani <shripha...@gmail.comwrote:
....
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.

Nov 6 '07 #2

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

Nov 6 '07 #3
On 11/6/07, Shriphani <sh********@gmail.comwrote:
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
Nov 6 '07 #4
On Nov 6, 3:09 pm, Ant <ant...@gmail.comwrote:
On Nov 6, 9:59 am, Shriphani <shripha...@gmail.comwrote:
...
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

Nov 6 '07 #5
On Nov 6, 10:19 am, Shriphani <shripha...@gmail.comwrote:
On Nov 6, 3:09 pm, Ant <ant...@gmail.comwrote:
On Nov 6, 9:59 am, Shriphani <shripha...@gmail.comwrote:
...
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
Nov 6 '07 #6
Shriphani wrote:
On Nov 6, 3:09 pm, Ant <ant...@gmail.comwrote:
>On Nov 6, 9:59 am, Shriphani <shripha...@gmail.comwrote:
...
>>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
Nov 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alistair | last post by:
Hi there, New to PHP. I have always used ASP. I am having trouble trying to do the things in PHP the way (or similar) i did it in ASP. In ASP I used an Access database. I now use a mySQL...
9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
6
by: rh0dium | last post by:
Hi all, Basically I have a bunch of pluggins in a directory (METDIR). For each one of these templated pluggins I want to do a specific routine. Let's start with a basic template file...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
1
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
0
by: bmerlover | last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender,...
11
by: inihility | last post by:
This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster. for (int i = 0; i < 50; i++) { a = getNumber(i); } Right now it...
2
by: msridhar87 | last post by:
hi, am doing a multi server chat program using select()..in the client side i need to multiplex thei/p from server and stdin. #include <stdio.h> #include <sys/types.h> #include <sys/socket.h>...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.