Python Forum

Full Version: nested for loop dilemma
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, so I have been doing some basic tasks on while loops for loops which I completed with some help here and there and I mostly grasped it without too much headache.
The task I have now is proving a major challenge but I think its because I haven't really worked with it before and its my first nested loops assignment.

So, I need to use a nested for loop and create the following numbers pyramid as described below:
I understand that the rows will be defined with the outer loop and the columns with the inner loop.
And the code starts something like this I believe:

for x in range(1,i+1):
Here is what I need to create:

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 47
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81

As yet I am just not getting it, I will really be for ever thankful if someone can give me guidance on this,
I do not want the code written for me, after all, I need to learn from this, I just need some pointers because this is driving me mad. Wall

Thanks in advance
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()
(Sep-11-2019, 04:44 PM)ichabod801 Wrote: [ -> ]
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

Hey ichabod801 thanks a stack for the speedy help you are my saviour! , I will go and try and complete it now and I will return with my coding attempt at the pyramid and post it here
One could start with observing pyramid and trying to find pattern(s).

We can observe that:

- first column is numbers from 1, 9
- number of items in row equals to the row number/first number in row (row #1 has 1 number, row #2 has two numbers .... row #9 has 9 numbers)
- numbers in row are multipliers of first number of row (a.k.a row number)

It follows that integers i with values 1...9 are emitted, then i is multiplied with values j in range 1, i + 1.

Visually it can be represented:

- > 1 (i) -> 1 * 1 (j)
- > 2 (i) -> 2 * 1 (j), 2 * 2 (j)
- > 3 (i) -> 3 * 1 (j), 3 * 2 (j), 3 * 3 (j)
...
(Sep-11-2019, 04:44 PM)ichabod801 Wrote: [ -> ]
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

Yay! haha..

for row in range(1,10):
    for column in range(1,row + 1):
         print(row * column , end = ' ')
    print()
        
its the first time I see "end" used like that in the inner print line, how does it work if its not defined in the code? neither do I see its coloured like other commands but yet it has a function? can you please explain how it works if you don't mind?
just for fun

for i in range(1,10):
    for n in range(i, i ** 2 + 1, i):
        print(n, end=' ')
    print() 

for i in range(1,10):
    print(' '.join(str(n) for n in range(i, i ** 2 + 1, i)))
    

# as oneliner    
print('\n'.join(' '.join(str(n) for n in range(i, i ** 2 + 1, i)) for i in range(1, 10)))
(Sep-11-2019, 05:45 PM)YoungGrassHopper Wrote: [ -> ]can you please explain how it works if you don't mind?
https://docs.python.org/3/library/functions.html#print
(Sep-11-2019, 05:29 PM)perfringo Wrote: [ -> ]One could start with observing pyramid and trying to find pattern(s).

We can observe that:

- first column is numbers from 1, 9
- number of items in row equals to the row number/first number in row (row #1 has 1 number, row #2 has two numbers .... row #9 has 9 numbers)
- numbers in row are multipliers of first number of row (a.k.a row number)

It follows that integers i with values 1...9 are emitted, then i is multiplied with values j in range 1, i + 1.

Visually it can be represented:

- > 1 (i) -> 1 * 1 (j)
- > 2 (i) -> 2 * 1 (j), 2 * 2 (j)
- > 3 (i) -> 3 * 1 (j), 3 * 2 (j), 3 * 3 (j)
...

Thanks for your guidance perfringo I appreciate it. I saw the pattern that part was very obvious to me but for some reason I could not lay in down in code, I believe its because this is my very first nested for loop task and its very new to me, I am being hit with a crazy amount of new info the last week I started with a software engineering bootcamp last Thursday and I have never coded prior to that, so really this is a whole new world to me, I am like a neathandral who just found fire for the very first time lol so if I sound very stupid its because Im a super noob and I am trying my best to stay above water, all of the advice I have received so far means a great deal to me you guys are top shelf

(Sep-11-2019, 05:52 PM)buran Wrote: [ -> ]
(Sep-11-2019, 05:45 PM)YoungGrassHopper Wrote: [ -> ]can you please explain how it works if you don't mind?
https://docs.python.org/3/library/functions.html#print

Hey buran Thanks for the sample code and info I appreciate it, I will have a look at it soonest : )
(Sep-11-2019, 05:50 PM)buran Wrote: [ -> ]just for fun

for i in range(1,10):
    for n in range(i, i ** 2 + 1, i):
        print(n, end=' ')
    print() 

for i in range(1,10):
    print(' '.join(str(n) for n in range(i, i ** 2 + 1, i)))
    

# as oneliner    
print('\n'.join(' '.join(str(n) for n in range(i, i ** 2 + 1, i)) for i in range(1, 10)))

Wow this is pretty cool, I am absolutely jealous , Thanks for enlightening me about the alternative ways to do it buran : )
This is a problem you need to work out on paper. You need to visualize what the loops are doing to solve it. As you analyze the pyramid, think of what is happening on each loop pass. Your outer loops count from 1 to the depth and the inter loop is a counter.
If I say anymore I'd give it away.

Draw it out on paper and if you figure out the solution you will have really learned something. It is a very important skill to figure out an algorithm to solve a problem. Be patient with it.

Work at it!
John