I'm attempting to create a number square when it's using a parameter. Whatever the user enters in the parameter it will display something like this (using 4 as an example):

4567
3456
2345
1234

I've come up this so far:

def numberedSquare(n):
    for a in range(n, 0, -1):
        print(a, end="\n")
    for i in range(n+1, 0+1, -1):
        print(i, end="\n")
    for j in range(n+2, 0+2, -1):
        print(j, end="\n")
    for f in range(n+3, 0+3, -1):
        print(f, end="\n")

Obviously the "\n" forces a new line but either way it still doesn't line up next to eachother. Now I'm thinking nested loops, but when I do, the numbers go crazy and so does the length of the square. :S

What am I missing?

Adam

Yes, you should have loop for line's start number and number running in one line.

Sorry, I struggle to follow. Could you elaborate a bit more please?

This is such a simple code it is difficult to not give complete ready solution if tell more. Give it yourself honest try.

The range of inner loop is guided by outer loop variable.

Note how the variables increase by one in this code (but in your example output they decrease??)

for a in range(n, 0, -1):
        print(a, end="\n")
    for i in range(n+1, 0+1, -1):
        print(i, end="\n")
    for j in range(n+2, 0+2, -1):
        print(j, end="\n")
    for f in range(n+3, 0+3, -1):
        print(f, end="\n")

You can use a for loop and increase the variable's value by the value of the for loop's variable.

I suggest to write another function numbered_line(n, start), then call this function.

But in this case actually you could do:

>>> for n in range(4567, 1000, -1111):
	print(n)

	
4567
3456
2345
1234
>>>

I like what you did there Tony, sadly it can't be used lol. As it has to work for whatever user has entered.

Thats another thing I'm struggling to get to grips with as well, 3 arguments within range on a for loop! Is there a dummy website that breaks it down and explains it? The zelle book doesn't talk much about multiple arguments and the worksheet provided doesn't exactly explain to my noob abilities.

Getting the hang of it, sort of. This is what I've got so for:

def numberSquare(n):
    for a in range(n, 0, -1):
        for b in range(n+1, 0+1, -1):
            print(a, end=" ")
        print()

ATM this is outputting the following:

4 4 4 4
3 3 3 3
2 2 2 2
1 1 1 1

Just can't seem to get the loop to add one on top of N each time and continually count downswards.


Update

I'm even closer!

def numberSquare(n):
    for a in range(n, 0, -1):
        for b in range(a+1, 0+1, -1):
                print(a, b, end=" ")
        print()

Output:

4 5 4 4 4 3 4 2 
3 4 3 3 3 2 
2 3 2 2 
1 2

Line loop goes up doesn't it:

>>> def numberSquare(n):
    for a in range(n, 0, -1):
        for b in range(a, a+n):
            print('%3s' % b, end=" ")
        print()

        
>>> numberSquare(4)
  4   5   6   7 
  3   4   5   6 
  2   3   4   5 
  1   2   3   4 
>>> numberSquare(14)
 14  15  16  17  18  19  20  21  22  23  24  25  26  27 
 13  14  15  16  17  18  19  20  21  22  23  24  25  26 
 12  13  14  15  16  17  18  19  20  21  22  23  24  25 
 11  12  13  14  15  16  17  18  19  20  21  22  23  24 
 10  11  12  13  14  15  16  17  18  19  20  21  22  23 
  9  10  11  12  13  14  15  16  17  18  19  20  21  22 
  8   9  10  11  12  13  14  15  16  17  18  19  20  21 
  7   8   9  10  11  12  13  14  15  16  17  18  19  20 
  6   7   8   9  10  11  12  13  14  15  16  17  18  19 
  5   6   7   8   9  10  11  12  13  14  15  16  17  18 
  4   5   6   7   8   9  10  11  12  13  14  15  16  17 
  3   4   5   6   7   8   9  10  11  12  13  14  15  16 
  2   3   4   5   6   7   8   9  10  11  12  13  14  15 
  1   2   3   4   5   6   7   8   9  10  11  12  13  14 
>>>

Irritating to think I've been stuck on just

for b in range(a, a+n):

So "a" is going to go through the loop as many times as n to 0 as, counting backwards. "b" is then created to repeat the sequence in the structure we want it (a square basically, n in width and height) and "a" is in the range of "b" because it's going to start whatever the value of "a" became, and the range is going to end at "a+n" together. Think i've got the jist of it, thank you. Am I wrong in this paragraph in here anything?

inner loops until one less, giving n values from a+0 to a+n exclusive.

If you understand list comprehensions, then this might be of some help:

>>> def numberedSquare(n):
    for j in range(n,0,-1):
        print (''.join([str(i) for i in range(j,j+n)]))


>>> numberedSquare(9)
91011121314151617
8910111213141516
789101112131415
67891011121314
5678910111213
456789101112
34567891011
2345678910
123456789
>>> numberedSquare(5)
56789
45678
34567
23456
12345
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.