Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magic Square Puzzle
#1
my question here
Hi I'm new to python and i have been tasked with creating a magic square puzzle. However the inputs for the magic square puzzle have to be provided through a file. This is the task:
 The program should prompt the user for the filename of a text file to open – some Python error handling should be used in case the user enters the name of a file that does not exist.

 The program should read the contents of the given text file and store this in a suitable Python data structure.

 Appropriate validation should be in place to ensure the supplied file contains a complete n x n square grid of digits, and ensure the digits are all integers in the range 1 to n2; a suitable error message should be returned where necessary.

 Assuming a suitable n x n square grid of integers is successfully loaded into the chosen data structure; further checks should then be performed to ensure that the sums of each row, each column, and each of the two diagonals are all equal.

 If any errors are detected in the puzzle board then details of these errors should be given to the user.

 If the puzzle board passes all the validation checks, then it should be written into a new text file using the same format as the original input file (see the example text files above) – the name for this new text file should be the original filename entered by the user prefixed with “VALID_”.
I would really appreciate it if anyone could help me with this task as I have been struggling.

Many Thanks

Further adding to this, this is the code i have so far:
fr = open('magicsquare1.txt', 'r')
#Calculating Totals for each row
for line in fr:
    total = 0
    line = line.split()
    c = len(line)
    print(line)

    for i in range(c):
        total = total +int(line[i])

print("total:", total)
Reply
#2
Break the problem into small pieces, and get each small piece working by itself.  Once you have that, you can combine them to have the overall project complete.

For example, I'd suggest ignoring file handling, user input, and parsing file contents.  Start with hard-coding a magic square, and write a function that checks whether or not it's valid.  Something like:
def is_square_valid(square):
    # I actually have no idea what a magic square is, so they all look invalid to me
    return False

test_square = [
    [2, 3, 4],
    [6, 1, 2],
    [1, 5, 3]
]

print(is_square_valid(test_square))
Once that works with various input, then you can start making things more complicated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,293 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  8 puzzle game aliyark145 4 10,618 May-30-2020, 05:54 PM
Last Post: Alkis
  A sign-Reversal Puzzle HY2000 2 2,434 Dec-05-2019, 11:55 AM
Last Post: HY2000
  Magic square! frequency 1 2,505 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Cross word puzzle solve using python constraint library aliyark145 1 3,247 Nov-29-2018, 10:53 AM
Last Post: Larz60+
  Square reverse sum(overloaded) shihomiyano 6 4,024 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Simple Eight-Puzzle not working! BenjaminDJ 2 3,131 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  Perfect Square program forumer444 4 8,877 Sep-01-2017, 09:32 PM
Last Post: forumer444
  Square Root on calculator MP1234593 1 7,859 Jun-06-2017, 06:58 PM
Last Post: nilamo
  Image Puzzle Digitalchemist 6 7,218 Jun-05-2017, 07:56 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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