Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loop dilemma
#1
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
Reply
#2
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
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)
...
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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?
Reply
#6
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)))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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 : )
Reply
#9
(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 : )
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,331 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,440 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,177 Nov-25-2020, 10:04 AM
Last Post: palladium
  dilemma with list comprehension spalisetty06 1 1,895 Aug-14-2020, 09:18 AM
Last Post: buran
  Nested loop indexing Morte 4 3,808 Aug-04-2020, 07:24 AM
Last Post: Morte
  Nested for loop not looping puttingwordstogether 0 1,671 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,833 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Nested Loop for user input Ads 2 3,524 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 5,609 Oct-16-2019, 03:11 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020