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


Messages In This Thread
Magic Square Puzzle - by Harnick - Aug-09-2017, 10:44 AM
RE: Magic Square Puzzle - by nilamo - Aug-09-2017, 04:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,467 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  8 puzzle game aliyark145 4 10,729 May-30-2020, 05:54 PM
Last Post: Alkis
  A sign-Reversal Puzzle HY2000 2 2,483 Dec-05-2019, 11:55 AM
Last Post: HY2000
  Magic square! frequency 1 2,564 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Cross word puzzle solve using python constraint library aliyark145 1 3,297 Nov-29-2018, 10:53 AM
Last Post: Larz60+
  Square reverse sum(overloaded) shihomiyano 6 4,116 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Simple Eight-Puzzle not working! BenjaminDJ 2 3,182 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  Perfect Square program forumer444 4 8,962 Sep-01-2017, 09:32 PM
Last Post: forumer444
  Square Root on calculator MP1234593 1 7,971 Jun-06-2017, 06:58 PM
Last Post: nilamo
  Image Puzzle Digitalchemist 6 7,330 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