Nested for loops

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
LVL 1
bnblazerAsked:
Who is Participating?
 
ramromconsultant Commented:
Put a comma at the end of all but the last print to suppress the newline that print supplies.
0
 
bnblazerAuthor Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Geert BormansInformation ArchitectCommented:
bnblazer,

Brian,
ignore my post
did not see you already concluded

cheers

Geert
0
 
ramromconsultant Commented:
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
 
ramromconsultant Commented:
Oh well I started my answer a while back, then went to meditate. Now I see you have come to the same conclusion.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.