473,386 Members | 1,720 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,386 software developers and data experts.

How to run 3 loops?

Thekid
145 100+
How would I write 3 for loops in the same code?

Expand|Select|Wrap|Line Numbers
  1.  
  2. for i in range(1,11):
  3.          i +=1
  4.          print i
  5.  
  6.  
Fine for one loop, but how do I get 2 more in there? I've tried this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. for i in range(1,11):
  3.         i +=1
  4. j=1
  5. for j in range(1,11):
  6.        j +=1
  7. k =1
  8. for k in range(1,11):
  9.        k +=1
  10.        print i,j,k
  11.  
  12.  
Doesn't work :( It can run either of this ways

1 1 1
2 2 2
3 3 3, etc

or this way, which I'd prefer:
...
8
9
10
....1 <---dots were just needed just to move the columns over in here
....2
....3...etc
.........1
.........2
.........3.....etc

maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. print i
  2. if i ==10:
  3.      print j
  4. if j==10:
  5.      print k
  6.  
???
Feb 21 '07 #1
3 1092
Hiya TheKid,

Try this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(1,11):
  2.     print i
  3.     for j in range(1,11):
  4.         print "\t%i" % j
  5.         for k in range(1,11):
  6.             print "\t\t%i" % k
  7.  
You MUST remember to indent each for loop by a consistent amount more than it's enclosing loop. Always be sure to use SPACES or TABS but DO NOT MIX THEM - USE ONE OR THE OTHER. Spaces (usually in blocks of four) are the preferred indentation method. If you mix them, you WILL get confusing errors.

hth,
-cybervegan
Feb 21 '07 #2
Thekid
145 100+
Thank you for the reply and advice. I tried your script and it didn't produce what I was attempting:

.....
8
1
2
3
4
5
6
7
8
9
10
9
1
2
3...etc. is how that came out but it using your script and "==" I got it:
Code:
[code]

for i in range(1,11):
print i
for j in range(1,11):
if i==10:
print "\t%s" % j
for k in range(1,11):
if j==10:
print "\t\t%s" % k

[\code]

Your tab ideas works........ Thanks again!
Feb 21 '07 #3
bvdet
2,851 Expert Mod 2GB
This may interest you:
Expand|Select|Wrap|Line Numbers
  1. def gen(s,e):
  2.     for x in range(s,e):
  3.         yield '%s' % x
  4.     for y in range(s,e):
  5.         yield '\t%s' % y
  6.     for z in range(s,e):
  7.         yield '\t\t%s' % z
  8.  
  9. for num in gen(1,11):
  10.     print num
Expand|Select|Wrap|Line Numbers
  1. def gen(s,e):
  2.     for x in range(s,e):
  3.         yield '%s\n' % x
  4.     for y in range(s,e):
  5.         yield '\t%s\n' % y
  6.     for z in range(s,e):
  7.         yield '\t\t%s\n' % z
  8.  
  9. nList = []
  10. for num in gen(1,33):
  11.     nList.append(num)
  12.  
  13. print "".join(nList)
Feb 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
6
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
2
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are...
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.