Python Forum
Program debugging (python)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program debugging (python)
#1
Hello, I am trying to create a program that asks user for sequence of 9 numbers and then verifies if they . make a magic square or not. Magic square requirments: rows, coumns and diagonal all sum up to 15 each, and no number is repeated. When I run and enter [1, 2, 3, 4, 5, 6, 7, 8, 9] I get an error code.

N = 3


# Returns true if mat[][] is magic
# square, else returns false.
def is_magic(square):
    # calculate the sum of
    # the prime diagonal
    sum = 0
    for i in range(0, N):
        sum= sum + square[i][i]

        # For sums of Rows
    for i in range(0, N):
        rowsum = 0
        for j in range(0, N):
            rowsum += square[i][j]

            # check if every row sum is
        # equal to prime diagonal sum
        if (rowsum != sum):
            return False

    # For sums of Columns
    for i in range(0, N):
        columnsum = 0
        for j in range(0, N):
            columnsum += square[j][i]

            # check if every column sum is
        # equal to prime diagonal sum
        if sum != columnsum:
            return False

    return True


str_input = input("write a squence of nine numbers")
str_list = str_input.split()



square = []
for i in range(0, N):
    square.append([])
    for j in range(0, N):
        square[i].append(str(str_list[j + i * N]))

if is_magic(str_list):
    print("This is Magic Square")
else:
    print("This is Not a magic Square")
Error:
File "/Users/abdullahali/Desktop/a1-q1.py", line 47, in <module> square[i].append(str(str_list[j + i * N])) IndexError: list index out of range
Reply
#2
I don't get that error. I get a later error on line 11, because you never convert the strings to integers. I think you want to replace the str call on line 47 to an int call. And you need to be careful about your input. If you don't input 9 numbers with spaces in between them, you'll get the error you got.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jan-17-2019, 04:01 AM)ichabod801 Wrote: I don't get that error. I get a later error on line 11, because you never convert the strings to integers. I think you want to replace the str call on line 47 to an int call. And you need to be careful about your input. If you don't input 9 numbers with spaces in between them, you'll get the error you got.

I replace str with int and now the line looks like this.
square[i].append(int(str_list[j + i * N]))
However, when i input 1 2 3 4 5 6 7 8 9 i get this error: for line 11
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Reply
#4
That's because you are checking is_magic(str_list). That should be is_magic(square).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jan-17-2019, 04:22 AM)ichabod801 Wrote: That's because you are checking is_magic(str_list). That should be is_magic(square).

Oh my god... i dont know how to thank you enough...
Reply
#6
(Jan-17-2019, 04:26 AM)abdullahali Wrote: i dont know how to thank you enough...

A duffel bag full of small bills usually works.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python debugging hw John0895 10 5,465 Oct-11-2018, 10:28 AM
Last Post: Phrotonz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020