?
Solved

Nested for loops

Posted on 2006-04-04
6
Medium Priority
?
522 Views
Last Modified: 2010-08-05
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
Comment
Question by:bnblazer
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 1500 total points
ID: 16371176
Put a comma at the end of all but the last print to suppress the newline that print supplies.
0
 
LVL 1

Author Comment

by:bnblazer
ID: 16371480
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

by:Geert Bormans
ID: 16371579
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16371594
bnblazer,

Brian,
ignore my post
did not see you already concluded

cheers

Geert
0
 
LVL 17

Expert Comment

by:ramrom
ID: 16371953
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

by:ramrom
ID: 16371969
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question