- 4th Jul 2024
- 17:39 pm
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in the figures below. The Lo Shu Magic Square has the following properties:
- The grid contains the numbers 1 through 9 exactly.
- The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figures A and B Below.
In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic Square. Test the function in a program.
Requirements
- Input Validation: Your program must throw an exception if any character other than 1 – 9 is selected. Your program should give some type of message explaining that the wrong input has been provided. Your program should prompt the user until the condition has been satisfied. It should prompt the user to enter a number from 1-9.
- Please make sure you include instructions in your program - When your program starts, it should explain how to play the game. Users who have never heard of the game should be able to play. The directions should be clear.
- Implement Turtle Graphics in your program - Use turtle graphics in your game
- Continuous game play - Your program should allow users to play the game until they choose to quit.
- Keep Track of Score
- Your program must track wins and losses (Hint: use a counter) You can use the turtle module to display your score
- Allow two gameplay options (Hint: elif)
o Option 1 - Allow user to choose list of numbers
o Option 2 - Set a random list of numbers
- Provide a detailed algorithm for your program - Your program should clear set of instructions on how you’ve planned to implement our code. Refer to chapter 2 for more information around algorithms.
- Provide comments for key components of your code - Use action words such as Get, Ask, Prompt, Calculate, Compute, Display, etc.
- Provide a flow chart for your code - Be sure to use relevant shapes for your flow chart. Find some online application to give your flow chart a professional look.
Lo Shu Magic Square - Get Assignment Solution
Please note that this is a sample solution created by our Python programmers for the Lo Shu Magic Square assignment. These solutions are for research and reference only.
- Visit our Python Assignment Sample Solution page to download the complete solution, including code, report, and screenshots.
- Connect with our Python Tutors for online tutoring to help you understand and complete this assignment.
- Check out the partial solution for this assignment in the blog post below.
Free Assignment Solution - Lo Shu Magic Square
// This is the matrix numbers given for the game
First = [
[4,9,2],
[3,5,7],
[8,1,6]
]
flag_h = True
flag_v = True
flag_d = True
def magic_square(lis):
sumline = 15
flag_h = hori(lis)
flag_v = vert(lis)
flag_d = dia(lis)
#print (flag_h)
#print (flag_v)
#print (flag_d)
if flag_h == True and flag_v == True and flag_d == True:
print ('This is a magic square, nice job!')
else:
print ('Try another one. This is not a magic square... ')
def hori(ls):
for i in ls:
current1 = 0
for k in i:
current1+=k
#print(flag_checkh(current1))
if flag_checkh(current1) == False:
return False
return True
def vert(ls):
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 0:
current3+=value
#this is a code for printing current3
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 1:
current3+=value
#this is a code for printing current3
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 2:
current3+=value
#this is a code for printing current3
return True
else:
return False
else:
return False
#this is a code for showing the diagonal
def dia(ls):
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 2:
current3+=value
#this is a code for printing current3
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 0:
current3+=value
return True
#this is a code for printing current3
else:
return False
def flag_checkh(n):
if n == 15:
flag_h = True
else:
flag_h = False
return flag_h
def flag_checkv(n):
if n == 15:
flag_v = True
else:
flag_v = False
return flag_v
def flag_checkd(n):
if n == 15:
flag_d = True
else:
flag_d = False
return flag_d
magic_square(First)
Get the best Magic Square Project assignment help and tutoring services from our experts now!
About The Author - Alex Johnson
Alex Johnson is a software developer and educator with extensive experience in Python and algorithm design. Alex enjoys creating interactive programming projects that make learning fun and accessible for students, such as the Lo Shu Magic Square. Passionate about teaching and problem-solving, Alex helps students develop strong coding skills and a solid understanding of computational concepts.