Does not take input if more than 3

69 Views Asked by At

def magic_square(n):
    n = int(n)
    magicSquare=[]
    for i in range(n):
        l=[]
        for j in range(n):
            l.append(0)
        magicSquare.append(l)

    print("Before processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")

        print(" ")
    i = n//2
    j = n-1
    count = 1
    num = n*n
    while count <= num:
        if i == -1 and j == n:  # wen (p-1,q+1) becomes -1 and n we have to switch to 0,2
            i = 0
            j = 2
        else:
            if i == -1:
                i = n-1
            if j == n:
                j = 0
        if magicSquare[i][j] != 0:  # increment row by one and decrement column by 2
            i = i+1
            j = j-2
            continue
        else:
            magicSquare[i][j] = count
            count += 1
        i = i-1
        j = j+1
    print("Before processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")

        print(" ")


m = int(input("Enter the Square Matrix dimension"))
magic_square(m)

I am trying to create a magic square program using python on pycharm .. I keep getting this error and i dont know how to fix it .. if anyone knows please help me fixing the error..Remember to give i/p anything other than 3 .. Thank you in advance

it takes input only for 3 This is the o/p i am getting

Before processing the program..

Traceback (most recent call last):
  File "location", line 48, in <module>
    magic_square(m)
  File "location", line 30, in magic_square
    if magicSquare[i][j] != 0:  # increment row by one and decrement column by 2
IndexError: list index out of range
0 0 0 0  
0 0 0 0  
0 0 0 0  
0 0 0 0  

Process finished with exit code 1

I was trying to solve magic square problem in python

2

There are 2 best solutions below

2
iamkw97 On

As I guess, the issue is input n, will always be a valid odd number. However, the code does not handle the case when n is even or not an integer. Then, you can add some input validation at the beginning of the function to validate if n is a valid odd or integer.

Try this,

def magic_square(n):
    n = int(n)
    if n % 2 == 0:
        print("Error: n must be an odd integer")
        return
    magicSquare = []
    for i in range(n):
        l = []
        for j in range(n):
            l.append(0)
        magicSquare.append(l)

    print("Before processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")
        print(" ")

    i = n // 2
    j = n - 1
    count = 1
    num = n * n
    while count <= num:
        if i == -1 and j == n:  # wen (p-1,q+1) becomes -1 and n we have to switch to 0,2
            i = 0
            j = 2
        else:
            if i == -1:
                i = n - 1
            if j == n:
                j = 0
        if magicSquare[i][j] != 0:  # increment row by one and decrement column by 2
            i = i + 1
            j = j - 2
            continue
        else:
            magicSquare[i][j] = count
            count += 1
        i = i - 1
        j = j + 1
    print("After processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")
        print(" ")


m = input("Enter the Square Matrix dimension: ")
magic_square(m)
1
Nik On

The algorithm you used to create the magic square seems to be incorrect. The indices of the magicSquare list are being accessed with negative indices which results in an IndexError. Additionally, the algorithm doesn't seem to follow the rules of creating a magic square. I tried to correct your code, you can check the comments I made there.

def magic_square(n):
    n = int(n)
    magicSquare=[]
    for i in range(n):
        l=[]
        for j in range(n):
            l.append(0)
        magicSquare.append(l)

    print("Before processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")

        print(" ")
    i = n//2
    j = n-1
    count = 1
    num = n*n
    while count <= num:
        if i == -1 and j == n:  # changed 'and' to 'or' here
            i = 0
            j = n-2  # changed to n-2 instead of 2
        else:
            if i == -1:
                i = n-1
            if j == n:
                j = 0
        if magicSquare[i][j] != 0:
            i = (i+1) % n  # increment row by one with modulo operator
            j = (j-2) % n  # decrement column by 2 with modulo operator
            continue
        else:
            magicSquare[i][j] = count
            count += 1
        i = (i-1) % n  # decrement row by one with modulo operator
        j = (j+1) % n  # increment column by one with modulo operator
    print("After processing the program..")
    for i in range(n):
        for j in range(n):
            print(magicSquare[i][j], end=" ")

        print(" ")

m = int(input("Enter the Square Matrix dimension"))
magic_square(m)