473,382 Members | 1,355 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,382 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 1107
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>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.