Infinite loop question

Question is : given an integer n, print a n*n matrix with squares whaving values n, then n-1, and …

Hello Muskan, so here we need to print the 5 in the outer layer then 4 inside that layer and then 3 and so on…
So basically we need to print n in the outer layer
n-1 in the second layer
n-2 in the third layer … and so on…

n = 5
for x in range(n):
    for y in range(n):
        print(max(x+1,y+1,n-x,n-y),end=' ')
    print()

It will return the max element from x+1,y+1,n-x,n-y
In python we can use max function to get the maximum value out of given some values such as for example if we have max(1,3,4,2,7,9,8,5,6) it will return value 9
So here we are focused with the max value at each step from the given inputs
so max(x+1,y+1,n-x,n-y) is there
for ex. of 3
if x = 0
and y = 0
then max(1,1,3,3) = 3
and y = 1
then max(1,2,3,2) = 3
and y = 2
then max(1,3,3,1) = 3

if x = 1
and y = 0
then max(2,1,2,3) = 3
and y = 1
then max(2,2,2,2) = 2
and y = 2
then max(2,3,2,1) = 3

if x = 2
and y = 0
then max(3,1,1,3) = 3
and y = 1
then max(3,2,1,2) = 3
and y = 2
then max(3,3,1,1) = 3

so overall it is
3 3 3
3 2 3
3 3 3

So I hope it is clear to you what we need to do.
In case you feel any doubt or confusion you are free to ask, I will surely help you out.
In case you understand this pls mark it as resolve and pls rate as well as provide the feedback so that we can improve our services.
Thanks :slight_smile:
Happy Coding !!

can you tell me the code properly i did not understood the answer you told

There are two loops in the code and n is initialised to 5
so n = 5
and I am iterating x from 0 to n-1 and for every value of x I am iterating y from 0 to n-1
Now we need to print the max element out of x+1,y+1,n-x,n-y and end=’ ’ means that after every element there must be the space and then in the next line we need to print the space for the next line.
And the code is this

n = 5
for x in range(n):
    for y in range(n):
        print(max(x+1,y+1,n-x,n-y),end=' ')
    print()

What else is the issue you are facing ?? If there is any pls let me know which part in the code you are not understanding