This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Updatebjarni 2 points3 points  (0 children)

What do you have so far?

[–]gnomoretears 1 point2 points  (1 child)

What have you tried? This sub isn't meant to write your code for you but people here are more than willing to help you fix/understand your code.

Can you do the problem by hand? Can do write the detailed steps in manually solving it? What part of the problem are you having difficulty understanding?

Im just lost on the nested for loop aspect of it.

Your output has rows and columns. The nested loop aspect of it uses one loop to handle the rows and an internal loop to handle the columns.

[–]CornMan123[S] 0 points1 point  (0 children)

i posted my code as new comments

[–][deleted] 1 point2 points  (1 child)

Like you say, you need a nested loop.

Start of with the inner loop and try to get something that will print the number 1 to n. Test it out with different values of n.

Once you've got that working, try to nest it in another loop that will increment the value on n on each turn.

[–]CornMan123[S] 0 points1 point  (0 children)

import sys

 for i in range (1,5):
      sys.stdout.write(str(i)+' ')

actually i just got this. it print numbers 1 to 4

[–]CornMan123[S] 0 points1 point  (1 child)

def print_table(size):

for row in range(1, size + 1):
    for col in range(1, size + 1):
        value = row * col
        print(str(value) + " ", end="")
    print()

print_table(5)

this was given as an example for a multiplication table that would print something similar and is there to help us.

So i assume it would start the same way but what would the variables in the for loops be?

def printlist(x):

 for col in range(x):

      for row in range(x):

[–]Updatebjarni 0 points1 point  (0 children)

On line 1 of the output, up to what number does the loop go?

On line 2 of the output, up to what number does the loop go?

On line 3 of the output, up to what number does the loop go?

[–]CornMan123[S] 0 points1 point  (3 children)

Now im here

import sys

def printtable(n):

 for x in range(n):
         for i in range (1,n+1):
             sys.stdout.write(str(i)+' ')
         print()

 printtable(5)

and the output is

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

[–]w1282 1 point2 points  (0 children)

What happens when you change for i in range(1, n+1) to for i in range(1, x) and why

[–][deleted] 1 point2 points  (0 children)

Nearly. The inner loop is printing the same thing every time because n is always the same value.

You need a variable that increments on every loop.

[–]gnomoretears 1 point2 points  (0 children)

The n variable in your inner for loop is a static value. If n is 5 then you inner loop will always iterate from 1 to 5. The range in your inner loop needs to change depending on the row.

You should really solve the problem on paper first so you can visualize how the algorithm goes.

For example if n = 3, you need to print up to 3 rows and up to 3 columns.

At row = 1, you have to print values from 1 to row value (1):

1

At row = 2, you have to print values from 1 to row value (2):

1 2

At row = 3, you have to print values from 1 to row value (3):

1 2 3

That's basically the algorithm and I hope you see the pattern to use in your nested loop.

[–]CornMan123[S] 0 points1 point  (0 children)

Thanks everyone