Link to home
Start Free TrialLog in
Avatar of bnblazer
bnblazer

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of ramrom
ramrom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bnblazer
bnblazer

ASKER

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
Avatar of Gertone (Geert Bormans)
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!
bnblazer,

Brian,
ignore my post
did not see you already concluded

cheers

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