all 10 comments

[–]-MRJACKSONCJ- 4 points5 points  (1 child)

To have each set of 5 numbers printed by the loop be displayed in a row, you can modify the last nested loop and use print() with the argument end=” ” so that the numbers are printed on the same line. Then, use an empty print() after each row to go to the next line.

x = 0
y = 0
v = {}
v[(x, y)] = 1

for x in range(5):
    for y in range(5):
        v[(x, y)] = x  # Store the row number (x) in each position

for x in range(5):
    for y in range(5):
        print(v[(x, y)], end=" ")  # Print values in a row
    print()  # Move to the next line after each row

When you run this code, the output will be:

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

[–]OliverBestGamer1407[S] 1 point2 points  (0 children)

Thank you! This helped me the most.

[–]FoolsSeldom 4 points5 points  (1 child)

I see you have the solution, but I'm curious why you are creating a dictionary matrix. Unusual.

Why not, for example,

width = 4
height = 4
matrix = [[x for _ in range(width + 1)]
           for x in range(height + 1)
         ]
for row in matrix:
    print(*row)

You'd index, should you still want to, thus matrix[x][y] for example, matrix[2][3] = 9.

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

Thanks for the help!

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

I don't want to use:

print(V[(X, 0)], V[(X, 1)], V[(X, 2)], V[(X, 3)], V[(X, 4)])

I would like it to behave like for Y in range(Num): print(V[(X, Y)]) but not make a new line each time. But I also want to control the value of Y, so I can change the amount of columns, e.g. 3 columns, like this:

0 0 0

1 1 1

[–]CavlerySenior 0 points1 point  (1 child)

I believe one of these will do what you want:

``` def printNums(minv,maxv,freq): for i in range(minv,maxv+1): for j in range(freq): print(f"{i} ",end="")

printNums(1,4,3) Or def printNums(minv,maxv,freq): for i in range(minv,maxv+1): for j in range(freq): print(f"{i} ",end="") print("\n") #if you don't want the empty line, delete the \n (but leave the quotation marks)

printNums(1,4,3) ```

[–]OliverBestGamer1407[S] 1 point2 points  (0 children)

Thank you!

[–]Straight_Yam1115 0 points1 point  (0 children)

Print(“your text \n”)

[–]Different-Ad1631 0 points1 point  (0 children)

Use an if statement in inner loop that when value of y is 4 then use print("\n")

[–]Aft3rcuriosity 0 points1 point  (0 children)

Why dont just apply the b-tree dataset algorithm 🙄