Solved

# Nested for loops

Posted on 2006-04-04
509 Views
I am trying to work through some old C++ assignments in Python to try and get a handle on it.  One of these assignments had to do with nested for loops.  What it did was print the pattern below:

*                    **********    **********                    *
**                  *********        *********                  **
***                ********            ********                ***
****              *******                *******              ****
*****            ******                    ******            *****
******          *****                        *****          ******
*******        ****                            ****        *******
********      ***                                ***      ********
*********    **                                    **    *********
**********  *                                        *  **********

I did it with the following C++ code:

#include <iostream>
using namespace std;

int main ()
{
int i, j;

//Loop through the other loops
for (i= 1; i< 11; i++) {

//print * and spaces for first block
for (j= 0; j< i; j++) cout << '*';
for (j= 0; j< 11- i; j++) cout << ' ';

//print * and spaces for second block
for (j= 0; j< 11- i; j++) cout << '*';
for (j= 0; j< i; j++) cout << ' ';

//print * and spaces for third block
for (j= 0; j< i; j++) cout << ' ';
for (j= 0; j< 11- i; j++) cout << '*';

//print * and spaces for fourth block
for (j= 0; j< 11- i; j++) cout << ' ';
for (j= 0; j< i; j++) cout << '*';

cout << '\n';
}
//pause
cout << "Hit enter:";
cin.get();

return 0;
}

I guess I am not thinking very pythonically here, but I cant seem to get it done.  I end up with a version that prints to thhe left column of the screen but throws a crlf in there and then prints the next block below.  Here is a partial code snippet:

for i in range (1,11):

for j in range (0,i):
print "*"*i
for j in range (0,11):
print " "*j

for j in range (0,11-i):
print "*"*i
for j in range (0,i):
print " "*j

Any input would be greatly appreciated.  My frustration meter is pegged and I can't believe I can't figure this one out for myself.

Brian
The embarrassed python newbie
0
Question by:bnblazer

LVL 17

Accepted Solution

Put a comma at the end of all but the last print to suppress the newline that print supplies.
0

LVL 1

Author Comment

Your ',' suggestion led me on the right path.  Thank you.  The code to make the above pattern is:

for i in range (1,11):
print "*"*i + " "*(11-i) + "*"*(11-i) + " "*i + " "*i + "*"*(11-i) + " "*(11-i) + "*"*i

Brian
0

LVL 60

Expert Comment

Hi bnblazer,

I bet this comes closer

for i in range (1,11):

for j in range (0,i):
print "*",
for j in range (0,11 - i):
print "  ",

for j in range (0,11-i):
print "*",
for j in range (0,i):
print " ",

print ''

as ramrom said, you need a comma
but on all statements
right before the end of the outer for you need a print '' without a comma, for the newline
and there is no need to do print "*"*j,
print "*" is enough, since you have the inner for

Cheers!
0

LVL 60

Expert Comment

bnblazer,

Brian,
ignore my post
did not see you already concluded

cheers

Geert
0

LVL 17

Expert Comment

Your code has other problems. Example:  "*"*i gives you a string of i asterisks. Doing that in a for loop gives you many more asterisks than you want.
Either drop the *i or the for.

Most pleasing to my eye would be:

for i in range(1,11): print "*"*i + " "*(11-i) + "*"*(11-i) + "  "*i + "*"*(11-i) + " "*(11-i) + "*"*i
0

LVL 17

Expert Comment

Oh well I started my answer a while back, then went to meditate. Now I see you have come to the same conclusion.
0

## Featured Post

### Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…